Lists
Lists
Watch this video:
Lists vs. vectors
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.
<- list(number = 1001, logical = TRUE, string = "stories")
things $number things
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
.
<- c(1, 2, 3, 4)
nums <- c(TRUE, TRUE, FALSE, TRUE)
logs <- c("apple", "banana", "carrot", "duck")
strs 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.
$strings df