R How to Construct a New Data Frame with a List-Variable (Example Code)

In this tutorial you’ll learn how to construct a new data frame with a list as column in the R programming language.

Example: Construct a Data Frame Containing a List Column

example_variable <- 10:13                                      # Creating a vector
example_variable                                               # Printing our vector
# 10 11 12 13
example_list <- list(1:2, letters[1:5], c("aa", "bb"), 33)     # Creating a list
example_list                                                   # Printing our list
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] "a" "b" "c" "d" "e"
# 
# [[3]]
# [1] "aa" "bb"
# 
# [[4]]
# [1] 33
example_data <- data.frame(example_variable, I(example_list))  # Creating a data.frame
example_data                                                   # Printing our data.frame
#   example_variable example_list
# 1               10         1, 2
# 2               11 a, b, c,....
# 3               12       aa, bb
# 4               13           33
example_data$example_list                                      # Printing the list column of our data.frame
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] "a" "b" "c" "d" "e"
# 
# [[3]]
# [1] "aa" "bb"
# 
# [[4]]
# [1] 33

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top