R stores data, and how that affects your workflowsEach piece of information is assigned of one class
Moving from highly-structured to less-structured
All elements in a vector are of an identical class
All elements in a vector are of an identical class
Vectors can be of any class introduced above
v1 <- c(1,2,3)
v2 <- c(4,5,6)
v3 <- c(7,8,9)
v4 <- c('a','b','c')
m1 <- matrix(c(v1,v2,v3), nrow = 3)
m2 <- matrix(c(v1,v2,v3,v4), nrow = 3)
m1
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
m2
## [,1] [,2] [,3] [,4]
## [1,] "1" "4" "7" "a"
## [2,] "2" "5" "8" "b"
## [3,] "3" "6" "9" "c"matrixName[rowNumber,columnNumber]dataframe[rowNumber,columnNumber]But we can also use the syntax dataframe$columnName
As with matrices and data frames, can extract individual vectors (columns or rows) by indexing the list
listName[[itemnumber]] or listName$itemName
v1 <- c(1,2)
v2 <- c(4,5,6)
v3 <- c(7,8,9,10)
v4 <- c('a','b','c', 'd', 'e')
l1 <- list(v1 = v1, v2 = v2, v3 = v3, v4 = v4)
l1
## $v1
## [1] 1 2
##
## $v2
## [1] 4 5 6
##
## $v3
## [1] 7 8 9 10
##
## $v4
## [1] "a" "b" "c" "d" "e"
class(l1$v1); length(l1$v1)
## [1] "numeric"
## [1] 2
class(l1[[4]]); length(l1[[4]])
## [1] "character"
## [1] 5https://swcarpentry.github.io/r-novice-inflammation/13-supp-data-structures.html
R, pt. 2Each piece of information is assigned of one class
This is a simplification; see here for advanced topics on data classes in R
Information organized into data structures
New today: Tibbles
v1 <- c(1,2,3)
v2 <- c("a","b","c")
v3 <- c(TRUE, TRUE, FALSE)
m <- matrix(c(v1, v1+3, v1+6), nrow = 3)
d <- data.frame(v1,v2,v3)
l <- list(v1, v2, v3, m, d)
l
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [1] "a" "b" "c"
##
## [[3]]
## [1] TRUE TRUE FALSE
##
## [[4]]
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## [[5]]
## v1 v2 v3
## 1 1 a TRUE
## 2 2 b TRUE
## 3 3 c FALSETibbles are a modern take on data frames. They keep the features that have stood the test of time, and drop the features that used to be convenient but are now frustrating.
data.frames into tibbles using as_tibble():R can have row names, but tibbles can not.mtcars dataset (inbuilt in R)tibble’s opinion: if it’s important, keep it as a column in your dataset.Example: print the mtcars dataframe (in-built in R)
Example: print mtcars as a tibble
The exception to the rule: values of size one are recycled
What if we wanted one of our columns to have vectors in it?
Let’s do some exercises
For beginners: https://swcarpentry.github.io/r-novice-gapminder/13-dplyr.html
For intermediate: https://beck-lab.ucdavis.edu/tutorials/purrr.html
For advanced: https://adv-r.hadley.nz/oo.html