R - tutorial 1 ### All data are stored locally in the directory where R-was started, unless it is not specified. ### Data are accessed by typing the name of the variable ### All functions are called by using parentheses ### Array elements are addressed by using brackets ### strings are enclosed in quotes (either single or double) ls() help(ls) save(file="R_mystuff") history() help.start() ### comments in R are preceded by a pound sign # ### to execute a file with R-functions or commands: source("file_name") ### setting scalar values: ### is a comment line a = 25 a <- 25 b = 23 c = a*b vectors x = 1:25 x = seq(34, 92, by=3) ### concatenation: x = c(1:12, 34:65) y = 34:65 ## or: z = c(x, y) ### multiply x by a scalar a x*a ### matrices ### cbind, rbind x = matrix(1:100, byrow=T, nrow=10) ### transpose: t(x) ### Accessing parts of a matrix x[,2] x[2,] ### submatrix: x[2:5,6:9] ### Functions: built in math ### sin(x) cos(x), exp(x), log(x), etc.... ### matrix multiplication x = matrix(1:6, 2, 3) y = 1:3 x %*% y ### This is NOT equal to x *y ### Reading a file X = scan(file="name_of_file") ### Z:/glees/data/GRAB/GEOL520/Data_Files X = scan(file="https://blackboard.unc.edu/courses/1/GEOL152.001.SPRING2005/content/_1357982_1/S1.res") X = scan(file="Z:/glees/data/GRAB/GEOL520/Data_Files/S1.res") X = scan(file="C:/Documents and Settings/lees/Desktop/S1.res") X = scan(file="Z:/glees/data/GRAB/GEOL520/Data_Files/xy.dat", list(x=0, y=0)) ### Structures ### structures in R are accessed through component names or indicies ### for example: X[[1]] X$x ### PLOTTING plot(X$x, X$y) ## add a title plot(X$x, X$y, type="n") points(X$x, X$y, pch=3) title(main="This is a wierd plot") #### make a plot but do not add data, omit the axis names x = rnorm(100) y = runif(100) X = list(x=x, y=y) plot(X$x, X$y, type="n", xlab='', ylab='') points(X$x, X$y, pch=3) title(main="This is a wierd plot") title(xlab="time,s", ylab="amplitude, N") #### make a plot but do not add data, omit the axis names plot(X$x, X$y, type="n", xlab='', ylab='', axes=FALSE) ### this created the plot and now we have to add to it points(X$x, X$y, pch=6) title(main="This is a wierd plot") title(xlab="time,s", ylab="amplitude, N") axis(1) axis(4) axis(2, at=seq(from=min(X$y), to=max(X$y), by=diff(range(X$y))/10 ) ) ### add in subtick marks axis(1, at=seq(from=min(X$x), to=max(X$x), by=.05), labels=FALSE, tck=0.005) axis(3, at=seq(from=min(X$x), to=max(X$x), by=.1), tck=-0.005) box() ################# ######### finding things on the plot: #### two main functions locator and identify locator(1) L = locator(5) L points(L$x, L$y, pch=2, col=3) ### for infinite number of points: L = locator() ############ click stop to end input L = locator(type="p", pch=8, col="blue") ########################################### I = identify(X$x, X$y) I points(X$x[I], X$y[I], col="purple", pch=1, cex=2) ############### plot(X$x, X$y, type="n", xlab='', ylab='') points(X$x, X$y, pch=3) title(main="This is a wierd plot") title(xlab="time,s", ylab="amplitude, N") symsize = rep(diff(range(X$x))/10, length(X$x[I])) symbols(X$x[I], X$y[I], circles=symsize, fg="purple", inches=FALSE, add=TRUE) symbols(X$x[I], X$y[I], circles=symsize, fg="purple", bg="green" , inches=FALSE, add=TRUE) ############### plot(X$x, X$y, type="n", xlab='', ylab='') points(X$x, X$y, pch=3) title(main="This is a wierd plot") title(xlab="time,s", ylab="amplitude, N") symsize = abs(rnorm(length(X$x[I]), mean=.1, sd=.03)) symbols(X$x[I], X$y[I], circles=symsize, fg="purple", bg="green" , inches=FALSE, add=TRUE) ############### distributions and probability ### empirical distribution of x par(mfrow=c(1,2)) hist(x) hist(y) par(mfrow=c(2,1)) hist(x) hist(y) ############### ## what is the probability of getting .2 or less in y? ## need to count all the points, need to count points less or equal to .2 ### take the ratio.... N = length(y) n = length(y[y<=0.2]) rat = n/N #### rat is about 12% #### source a file with R commands source("d:/LEES/CLASSES/GEOL_520 Data_Analysis/LAB_NOTES/RsourceTest.txt") ####################### ### functions ## create a function to get the dot product of two vectors dot<-function(x,y) { A = x*y B = sum(A) return(B) }