Convert Data Frame Columns to List Elements in R (2 Examples) | Adapt One / Each Variable | colnames

Published: 21 July 2021
on channel: Statistics Globe
1,993
21

How to transform data frame variables to list elements in the R programming language. More details: https://statisticsglobe.com/convert-d...
R code of this video:

data <- data.frame(x1 = 5:9, # Create example data
x2 = letters[2:6],
x3 = "X")

my_list1 <- list(data$x1) # Convert one column to list element
my_list1 # Print list

my_list2 <- list() # Create empty list

for(i in 1:ncol(data)) { # Using for-loop to add columns to list
my_list2[[i]] <- data[ , i]
}

names(my_list2) <- colnames(data) # Rename list elements

my_list2 # Print list

Follow me on Social Media:
Facebook:   / statisticsglobecom  
LinkedIn:   / statisticsglobe  
Twitter:   / joachimschork  


Watch video Convert Data Frame Columns to List Elements in R (2 Examples) | Adapt One / Each Variable | colnames online, duration hours minute second in high quality that is uploaded to the channel Statistics Globe 21 July 2021. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 1,993 times and liked it 21 visitors.