# Essential R ### What you need to get started #### Eric Nord ##### Assistant Professor of Biology, Greenville College ##### Fulbright Scholar, Lilongwe University of Agriculture and Natural Resources, Malawi ## Chapter 1: Basics ### I: The Terminal ---- 2 + 4 # this was 2+3 4*5 6^2 3+5*2 (3+5)*2 a = 5 a a*2 a a=a*2 # must *assign* to change a value # = and <- and -> # objects in R exist in your *workspace* # console vs editor # Editor - multiple files open - R script file contains R code and # comments - not output unless pasted behind comments - easily run code # from the editor - easy to save # Open an new script file and type "# editor demo". Now press # Ctrl+Enter, and R will run that command (since it is a comment, it # will just be writen to the console). Now type "a*4" and press Ctrl+Enter. # console not easily be saved - contains the output from your R code - # up arrow to retrieve previous commands ### II: Working with Vectors ---- sms=c(0,1,2,0,0,0,1) sms + 5 sms * 5 sms/2 sort(sms) sum(sms) sum(5,6,7) ### III. Subsetting Vectors - the magic "[]" ---- sms[3] sms[2:4] sms[-3] sms[-(2:3)] sms[-c(2,3,7)] 5:9 # logical extraction sms>0 sms[sms>0] # FALSE=0 and TRUE=1. sum(sms>0) which(sms>0) sms[which(sms>0)] mean(sms[which(sms>0)]) sms sms[1]=1 sms a=sms; a[which(a==0)]<-1; a # note == vs = 1:50 # note the [1], etc. x=c("a","b","c","d","e","f","g","h","i") y=21:30 z=c(2,4,6) x[y] # NA b/c no 21-30th elements x[y-20] # only one NA b/c no 10th element x[z] x[rev(z)] # try rev(z) y[z] z[y] y[x] # NA b/c the "ath" element of y doesn't make sense y*z # note warning -- recycling! ### IV. Other Useful Functions ---- # sd() # standard deviation # median() # median # max()# maximum # min()# minimum # range() #maximum and minimum # length() # number of elements of a vector # cummin() # cumulative min (or max cummax()) # diff() # differences between successive elements of a vector # note + rather than output or > # auto matching in Rstudio # : ### V. Exercises. ---- # 1) Enter the following data in R and call it P1: # 23,45,67,46,57,23,83,59,12,64 # What is the maximum value? What is the minimum value? What is the mean value? # # 2) Oh no! The next to last (9th) value was mistyped - it should be 42. # Change it, and see how the mean has changed. How many values are # greater than 40? What is the mean of values over 40? # Hint: how do you see the 9th element of the vector `P1`? # # 3) Using the data from problem 1 find: # a) the sum of P1 # b) the mean (using the sum and length(P1)) # c) the log(base10) - use log(base=10) # d) the difference between each element of P1 and the mean of P1 # # 4) If we have two vectors, a=11:20 and b=c(2,4,6,8) describe (without # running the code) the outcome of the following (check yourself by # running the code): # a) a*2 # b) a[b] # c) b[a] # d) c(a,b) # e) a+b