Getting started with R: The information for these R_intro files has been obtained from the R help system, including the books "An Introduction to R", "Rlanguage definition", R Data import/export". Introductory files and reference cards have been obtained by R.Boik, K. Newman, and J Robinson-Cox. Just type in the examples and follow what happens. Obtaining R: R can be downloaded from http://lib.stat.cmu/R/CRAN/ Installing R: Run rw2001.exe from wherever you saved it and follow instructions from Setup Wizard. The source code is available for programers. Running R: It is a good idea to create a subdirectory for each problem you work on. The workspace will rapidly get cluttered and slow your computations, and mixing projects can lead to serious mistakes. Start R by clicking on the program icon onthe desktop, or by using the Windows start menu. Change to the desired working directory with the command setwd("yourharddrive:\\yourpath\\yoursubpath") Note that you cannot use "\" to specify the path, only "\\" or "/" will work. Saving your workspace: save.image(file="mystuff.RData") saves all of your current objects to the file "mystuff.Rdata". Also saved in a file called ".Rhistory" is your command history Quitting R: q() quits and asks you whether you want to save your workspace. q("yes") quits and saves workspace. q("no") quits and does not save workspace. Restarting R: If you double click on the .RData file for a project, R will be started and your workspace reloaded, including your setting ofro the working directory Getting Help in R: for a function, type help(functionname) and a new window with a function description, required arguments and examples appears. Often these files are linked to other help files and following these links is useful. help.start() opens your browser to a R help page. This give you access to anumber of manuals in HTML In the www.r-project.org web pages, you can find pdf manuals also. Because many mailing lists of "how to do" problems are on the web, sometimes it helps to google an R related question. Now, practice the commands below and carefully look at what R gives you # Simple arithmetic functions #1. element by element x <- 3:12 abs(x) log(x) log10(x) sqrt(x) exp(x) sin(x) #2. operate on entire vector: # returns a scalar value sum(x) prod(x) #multiply each term #return vector of same length cumsum(x) #x[1], x[1]+x[2], x[1]+x[2]+x[3] cumprod(x) #x[1], x[1]*x[2], x[1]*x[2]*x[3] #3. conversion to integers, rounding,.. x <- 103.652 round(x) round(x,1) round(x,2) trunc(x) #nearest integer in direction of 0 trunc(-0.8) floor(x) #nearest integer in direction of negative infinity floor(-0.8) ceiling(x) #nearest integer in direction of positive infinity ceiling(-0.8) #4. max, min related x <- c(3,5,5,9) y <- c(5,3,5,11) max(x) min(x) range(x) #useful for setting limits on graph axes pmax(x,y) #per position [i] return maximum of x[i] and y[i] pmin(x,y) #5. ordering x <- c(12,5,6,6,-1) sort(x) rev(x) order(x) #returns position of smallest, next smallest, ... rev(order(x)) #returns position of largest, next largest,... rank(x) #rank of x[1], then rank of x[2]; useful for ranksum statistics #6. simple statistics x <- c(8,12,5,4,19,2,1) y <- rnorm(length(x),mean=3+2*x,sd=1) mean(x) var(x) median(x) quantile(x,probs=c(0.25,0.75)) summary(x) #a generic function used on lots of classes of data # Arithmetic and logical expressions #0. Precedence #See Table 2.1 on p. 33 of V&R, but some common situtations, highest to lowest: function(..) ^ #exponentiation * / #multiplication, division + - #addition, subtraction > < <= == != #comparison operators #but can always use parentheses to force a precedence x <- 36 y <- 7 sqrt(x)*7-2 < x+y^2 (((sqrt(x))*7)-2) < (x+(y^2)) #identical to above but "clear" precedence #1. element by element operations on two vectors x <- 1:10 y <- 11:20 x+y x-y x*y x/y x^y #2. recycling: when operating on 2 vectors with unequal length # length of output = length of longer vector, recycling the # shorter vector's values till match longer vector's length # Note!!! This can have unintended consequences if your vectors # are of unequal length because of an error in construction x <- 2 y <- 1:6 x+y #2+1, 2+2, 2+3, 2+4, 2+5, 2+6 x <- c(2,9) x+y #2+1, 9+2, 2+3, 9+4, 2+5, 9+6 #3. Integer division and modulo reduction 7 %/% 3 #answer 2, number of times 3 goes into 7 7 %% 3 #answer 1, 7 - (7 %/% 3)*3 #4 Logical expressions and functions with logical arguments x <- c(1,3,5) y <- c(2,2,4) x < y x <= y x != y any(x < <= == != #comparison operators #but can always use parentheses to force a precedence x <- 36 y <- 7 sqrt(x)*7-2 < x+y^2 (((sqrt(x))*7)-2) < (x+(y^2)) #identical to above but "clear" precedence #1. element by element operations on two vectors x <- 1:10 y <- 11:20 x+y x-y x*y x/y x^y #2. recycling: when operating on 2 vectors with unequal length # length of output = length of longer vector, recycling the # shorter vector's values till match longer vector's length x <- 2 y <- 1:6 x+y #2+1, 2+2, 2+3, 2+4, 2+5, 2+6 x <- c(2,9) x+y #2+1, 9+2, 2+3, 9+4, 2+5, 9+6 #3. Integer division and modulo reduction 7 %/% 3 #answer 2, number of times 3 goes into 7 7 %% 3 #answer 1, 7 - (7 %/% 3)*3 #4 Logical expressions and functions with logical arguments x <- c(1,3,5) y <- c(2,2,4) x < y x <= y x != y any(x