Lists

Lists

Watch this video:

Lists vs. vectors

Which data structure(s) could you use to store these pieces of data in the same object? 1001, TRUE, "stories"




Make a list

Make a list that contains the elements 1001, TRUE, and "stories". Give each element a name.

list(number = 1001, logical = TRUE, string = "stories")

Extract an element

Extract the number 1001 from the list below.

things <- list(number = 1001, logical = TRUE, string = "stories")
things$number

Data Frames

You can make a data frame with the data.frame() function, which works similar to c(), and list(). Assemble the vectors below into a data frame with the column names numbers, logicals, strings.

nums <- c(1, 2, 3, 4)
logs <- c(TRUE, TRUE, FALSE, TRUE)
strs <- c("apple", "banana", "carrot", "duck")
data.frame(numbers = nums, logicals = logs, strings = strs)

Good job. When you make a data frame, you must follow one rule: each column vector should be the same length

Extract a column

Given that a data frame is a type of list (with named elements), how could you extract the strings column of the df data frame below? Do it.

df$strings

Next topic