Cheatography

# R Basics Cheat Sheet by bwaldo

### Vectors

 The c() function can be used to create vectors of objects x <- c(0.5, 0.6) ## numeric x <- c(TRUE, FALSE) ## logical x <- c(T, F) ## logical x <- c("a­"­,"b",­"­c") ## character x <- 9:29 ## interger x <- c(l+0i, 2+4i) ## complex Using the vector() function > x <- vector­("nu­mer­ic", length = 10) When different objects are mixed in a vector, coercion occurs so that every element in the vector is of the same class y <- c(1.7,­"­a") ## character y <- c(TRUE, 2) ## numeric y <- c("a­", TRUE) ## characte Objects can be explicitly coerced from one class to another using the "­as.*­" functions if availa­ble > x <- 0:6 ## create a sequence of 0-6 > class(x) ## class of the object  "­int­ege­r" > as.num­eric(x) ## change the class of the object to numeric  0 1 2 3 4 5 6 > as.log­ical(x) ## change the class of the object to logical  FALSE TRUE TRUE TRUE TRUE TRUE TRUE > as.cha­rac­ter(x) ## change the class of the object to character  "­0" "­1" "­2" "­3" "­4" "­5" "­6" > as.com­plex(x)  0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i Nons­ensical coercion results in NA's > x<- c("a­"­,"b",­"­c") > as.num­eric(x)  NA NA NA Warning message: NAs introduced by coercion > as.log­ical(x)  NA NA NA > as.com­plex(x)  NA NA NA

### Lists

 Lists - Lists are a special type of vector that can contain elements of different classes. Lists are a very IMPORTANT data type in R. > x <- list(1­,"a",­TRUE, 1 +4i) ##list function > x [] ##vector element 1  1 [] ##vector element 2  "­a" [] ##vector element 3  TRUE [] ##vector element 4  1+4i

### Matrices

 Matrices are vectors with a dimension attribute. The dimension attribute is itself an integer vector of length 2(nrow­,nc­ol) > m <- matrix­(nrow = 2, ncol = 3) #matrix function > m [,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA > dim(m)  2 3 > attrib­utes(m) \$dim  2 3 Matrices are constr­ucted column­-wise, so entries can be thought of starting in the "­upper left" corner and running down the columns > m <- matrix­(1:6, nrow = 2, ncol = 3) # create a matrix with a sequence of 1-6 > m [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 Matrices can also be created directly form vectors by adding a dimension attrib­ute > m <- 1:10 # create a vector that is a sequence from 1-10 > m  1 2 3 4 5 6 7 8 9 10 Adding the dim function but assigning and attribute to m, take this vector and transform it into a matrix that is 2 rows and 5 columns > dim(m) <- c(2,5) > m [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 Matrices can be created by column­-bi­nding cbind() or row-bi­nding rbind() > x<-1:3 > y<-­10:12 > cbind(x,y) x y [1,] 1 10 [2,] 2 11 [3,] 3 12 > rbind(x,y) [,1] [,2] [,3] x 1 2 3 y 10 11 12

 A quick and dirty way to figure out the classes of each column is the follow­ing: * initial <- read.t­abl­e("d­ata­tab­le.t­xt­", nrows = 100) * classes <- sapply­(in­itial, class) * tabALL <- read.t­abl­e("d­ata­tab­le.t­xt­", colClasses = classes) * Set nrows. This doesn't make R run faster but it helps with memory usage

### Textual Data Formats

 * dput(): writes R code to which can be used to recons­truct a R object. * dget(): single R objects * dump(): similar to dget but can be used on multiple R objects. dput() example > y <- data.frame(a=1,b="a") > dput(y) # output to console struct­ure­(list(a = 1, b = struct­ure(1L, .Label = "­a", class = "factor")) , .Names = c("a­", "­b"), row.names = c(NA, -1L), class = "data.frame") > dput(y, file ="y.R­") # save to a file named y.R > new.y <- dget("y.R") # get the file > new.y a b 1 1 a
dump() example

> x <- "foo"

> y <- data.frame(a=1,b="a")

> dump(c­("x",­"­y"), file = "­dat­a.R­") # paSS the objects x and y and create and store in data.R

> rm(x,y) # remove them from R

> source­("da­ta.R­") # calls them back into R

> y

a b

1 1 a

> x

 "foo"

### Factors

 Factors are used to represent catego­rical data and can be unordered or ordered. One can think of a factor as an integer vector where each integer has a label > x <- factor­(c(­"­yes­"­,"ye­s", "­no" , "­yes­", "­no")) > x  yes yes no yes no Levels: no yes > table(x) ##if you table x then it will tell how many of each value there are x no yes 2 3 > unclass(x)  2 2 1 2 1 attr(,­"­lev­els­")  "­no" "­yes­" The order of the levels can be set using the levels argument to factor(). This can be important in linear modelling because the first level is used as the baseline level So in the example below we set the levels of c because default would put no first and we want yes first > x <- factor­(c(­"­yes­"­,"ye­s", "­no" , "­yes­", "­no")­,levels = c("y­es",­"­no")) > x  yes yes no yes no Levels: yes no

### Missing Values

 Missing values are denoted by NA or NaN for undefined mathem­atical operat­ions. * is.na() is used to test objects if they are NA * is.nan() is used to test for NaN * NA values have a class also, so there are interger NA, character NA etc * a NaN value is also NA but the converse is not true > x <- c(1,2,­NA,­10,3) > x  1 2 NA 10 3 > is.na(x)  FALSE FALSE TRUE FALSE FALSE > is.nan(x)  FALSE FALSE FALSE FALSE FALSE > x <- c(1,2,­NaN­,NA,4) > is.na(x)  FALSE FALSE TRUE TRUE FALSE > is.nan(x)  FALSE FALSE TRUE FALSE FALSE

### Names Attribute

 R objects can also have names, which is very useful for writing readable code and self-d­esc­ribing objects > x <- 1:3 > names(x) NULL > names(x) <-c("foo","bar","norf") > x foo bar norf 1 2 3 > names(x)  "­foo­" "­bar­" "norf" List can also have names > x <- list(a=1,b=2,c=3) > x \$a  1 \$b  2 \$c  3 Matrices can have names > m <- matrix­(1:4, nrow=2, ncol=2) ## create a matrix sequence 1-4 > dimnam­es(m) <- list(c­("a",­"­b"), c("c","d")) > m c d a 1 3 b 2 4

### Data Frames

 Data frames are used to store tabular data. * They are repres­ented as a special type of list where every element of the list has to have the same length. * Each element of hte list can be thought of as a column and the length of each element of the list is the # or rows. * Unlike matrices, data frames can store different classes of objects in each column (just like lists) matrices must have every element be the same class. * Data frames also have a special attribute called row.names * Data frames are usually created by calling read.t­able() or read.csv() * Can be converted to a matrix by calling data.matrix() > x <- data.f­ram­e(foo = 1:4, bar = c(T,T,F,F)) > x foo bar 1 1 TRUE 2 2 TRUE 3 3 FALSE 4 4 FALSE > nrow(x)  4 > ncol(x)  2

### Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us! 