Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data: Lists and Dataframes

Similar presentations


Presentation on theme: "Data: Lists and Dataframes"— Presentation transcript:

1 Data: Lists and Dataframes
Beau Benjamin Bruce, MD, PhD Analytics Lead, Enteric Diseases Epidemiology Branch DFWED/NCEZID/CDC

2 New food: list Lists allow you to mix different types of atomic data together list(“A”, 2, TRUE) [[1]] [1] "A" [[2]] [1] 2 [[3]] [1] TRUE

3 New food: list Can even include other composite data types
list(“A”, c(1,2,3,4), TRUE) [[1]] [1] "A" [[2]] [1] [[3]] [1] TRUE

4 New food: list How to access the elements of a list
mylist <- list(a=“A”, b=c(1,2,3,4), c=TRUE) mylist[[1]] => “A” mylist[[2]] => What if wanted to get the 3 out alone??

5 New food: list How to access the elements of a list
mylist <- list(a=“A”, b=c(1,2,3,4), c=TRUE) mylist[[2]] => mylist[[2]][3] => 3

6 List accessors for list ‘train’
A list (train) made only of the first car train[c(1,2)] A list made of the first and second car train[3] A list made of the third car

7 List accessors for list ‘train’
A list made of the second car train[[2]] The contents of the second car: a vector of cargo containers (CCs) train[[2]][2] The second CC in vector of CCs in the second car

8 How about a list of lists: trainb?
A list made of the first element of trainb (which contains another list) A list made of the second car trainb[[1]] The contents of the first element of trainb: a list with two cars train[[2]][2] trainb[[1]][1] The list made from the first element of the list extracted just above

9 How about a list of lists: trainb?
Same list we extracted from first care of trainb on previous slide A list made of the second car trainb[[1]][[1]] Now extract the contents of its first element: a vector of CCs trainb[[1]][[1]][2] train[[2]][2] And take the second element of that vector of CCs: a single CC

10 New food: list Can name the elements list(a=“A”, b=c(1,2,3,4), c=TRUE)
[1] $c [1] TRUE

11 New food: list Can use $ to access by name (another very shy creature!) mylist <- list(a=“A”, b=c(1,2,3,4), c=TRUE) mylist$b => mylist$b[2] => 2

12 New food: list Like a vector can change a part
mylist <- list(a=“A”, b=c(1,2,3,4), c=TRUE) mylist$b[2] <- 4 mylist$b => ??? is.list(mylist) => ???

13 New food: data.frame A special list whose elements (typically vectors) all have same length (i.e., the same number of observations) data.frame(age=c(3,2,3,3,1,2,4,4), sorethroat=factor(c(“y”,”y”,”y”, “y”,”n”,”n”,”n”,”n”))) two variables, 8 observations

14 New food: data.frame Usually will create data.frames by importing them from other files (e.g., CSV, Excel, SAS data files) Show the variable names: names(iih_data) Access variable by name with $ like a list: iih_data$sex

15 New food: data.frame Access row by number: Access column by number:
iih_data[3, ] Access column by number: iih_data[, 3] Access rows by test: iih_data[iih_data$sex == “M”, ] Change a value iih_data$sex[3] <- “F”

16 class function Can be used instead of the is.xxxx functions
Returns what type of object something is, but may not tell you the whole story: class(iih_data) => “data.frame” is.data.frame(iih_data) => TRUE is.list(iih_data) => TRUE


Download ppt "Data: Lists and Dataframes"

Similar presentations


Ads by Google