in free beer” • “as in free speech” • use it for any purpose. • give copies to your friends & neighbours. • improve it and release improvements publicly.

is command-driven • R will not tell you what to do, or guide you through the steps of an analysis or method. • R will do all the calculations for you, and it will do exactly what you tell it (not necessarily what you want). • R has the flexibility and power to do exactly what you want, exactly how you want it done.

• Navigate the R(studio) interface • Enter commands • input & output • common functions • Control stuctures • Use technical terms for R concepts • Get Help

using an assignment operator • Variable names can include: • letters a-z A-Z • numbers 0-9 • periods . • underscores _ • Variable names should begin with a letter A <- 10 B <- 10*10 A_log <- log(A) B.seq <- 1:B <- assign the value on the right to the name on the left Objects

a vector • Think of a vector as a list of related values (data) • A single value is an "atomic vector" (vector with a length of 1) [1] 2 1:10 Vectors index: the item number value (result) [1] 1 2 3 4 5 6 7 8 9 10

• Vectors can be used in a plot. • You can access an element of a vector by its index my_fav_nums<-c(1, 4, 10, 444, 42) Vectors plot(1:5, my_fav_nums) my_fav_nums[3] [1] 10

types • character (string) • in single ' or double " quotes. > 'hello world' > "1.23" • logical • TRUE or FALSE converting from one type to another = "coercion"

the R language (keywords) and should never be used as variable names Reserved words NA "Not Available" (unknown or missing data) NaN "Not a Number" (undefined numeric values) NULL a special object (missing objects) Inf Infiniti TRUE Logical value FALSE Logical value T short for TRUE F short for FALSE c,q,t,C,D,I R functions diff, df, pt R functions

values: TRUE or FALSE == "equal": Note the two equals signs. Not to be confused with a single equals sign (used to assign values). != "not equal" > "greater than" < "less than" >= "greater than or equal to" <= "less than or equal to"

understand, or cannot execute, it outputs an error to the console. Error in 1 + "2" : non-numeric argument to binary operator Fail <- 1 + "2" Errors Error: object 'fail' not found Fail

(or the developers) think is "ideal", it may produce a warning instead. • Use the warnings() command to review them. Warning message:In log(-1) : NaNs produced oops <- log(-1) Warnings

use a function (call), the command must be structured properly, following the "grammar rules" of the R language (syntax) log( 8 , base = 2 ) Functions function name

use a function (call), the command must be structured properly, following the "grammar rules" of the R language (syntax) log( 8 , base = 2 ) Functions function name parentheses no space

use a function (call), the command must be structured properly, following the "grammar rules" of the R language (syntax) log( 8 , base = 2 ) Functions function name argument 1 parentheses no space

use a function (call), the command must be structured properly, following the "grammar rules" of the R language (syntax) log( 8 , base = 2 ) Functions function name argument 1 comma parentheses no space

use a function (call), the command must be structured properly, following the "grammar rules" of the R language (syntax) log( 8 , base = 2 ) Functions function name argument 1 comma parentheses argument 2 no space

when it is called • Arguments are values and instructions the function needs to do its thing Ex: Arguments x<-1:10 y<-sin(x) plot(x,y,type=‘l’) arguments

sum mean sd var summary plot par paste format head length str names typeof class attributes library ls rm setwd getwd file.choose (Mac) choose.file (PC) c seq rep tapply aggregate merge cbind rbind unique help ? help.search ?? help.start

& paste Examples into the console to see the function in action. Try to modify the example code to do what you want. Va lu e returned You can also use example(seq) in the console to run all the example code in this section. Details

information in R vector a combination of values, of the same type. list a combination of different types of values (or even objects) data frame a collection of vectors of the same length (# rows) columns = “variables” ; rows = “cases”

structure of the object names of items in the object attributes of the object summary statistics plot of all variable combinations data(CO2) head(CO2) str(CO2) names(CO2) attributes(CO2) summary(CO2) plot(CO2) Working with a data frame

object by their index or name (if they have one) CO2$Treatment Indexing CO2[1:6,3] object name r ow s (dim. 1) colu m n s (dim. 2) object name $ operator column name

CO2[sample(nrow(CO2), 10),] available names "Treatment" column all rows, column 3 row 3, all columns rows 1-6, all columns rows 1-6, column 3 elements 1-6 of Treatment rows where conc > 100 rows where Treatment == “chilled" 10 random rows

100, but if the number is a multiple of 3, print ‘fizz’ instead. 2) Extend your code so that for multiples of 5, it prints out ‘buzz’. HINT: To get the remainder of integer division, use %%. Ex: 4 9%%5

base functions in R, you can install additional packages to do specialized statistics and plotting. • Currently, the CRAN package repository features 4276 available packages. • http://cran.r-project.org/web/packages/

Some plots and graphs that can be made using R • images and other graphics made using R • a demonstration of linear modelling & GLMs • a list of available demos

the variable ‘x’ as an argument in a function or expression • Assign the result to the same variable ‘x’ • How long can you keep the chain going without getting errors? x <- 0 Let’s play “Command-R” 54

a : • vector of multiple items • data frame • Use x in a graph / plot x <- x + 1 x <- x * (x+10) x <- exp(x) x <- 1:x x <- seq(from=x, to=100, by=2) x <- rnorm(x) x <- x[1:3] x <- x[2] x <- data.frame( foo = rnorm(length(x)), x) “Command-R” 55