Home > Enterprise >  How to unlist dataframes within a list into several dataframes itself?
How to unlist dataframes within a list into several dataframes itself?

Time:03-11

I would like to bring my dataframes within a list out of the list to have several dataframes at the end. Additionally I would like to give them automatically the name which they had before I read them into a list, with adding an ending name ".done" for example and write.table() back into my folder where the original data comes from for every dataframe.done in my environment. Here my example data

library(dplyr)
set.seed(94756)
mat1 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5) 
mat1 <- as_tibble(mat1)

mat2 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)  
mat2 <- as_tibble(mat2)

mat3 <- matrix(sample(seq(-1,100, 0.11), 50,replace = TRUE),ncol = 5)  
mat3 <- as_tibble(mat3)

data <- list(mat1, mat2, mat3)

Thanks in advance!

CodePudding user response:

Make the list based on names (and it will be a named list this way)

data = mget(ls(pattern = "mat\\d ")) ## use a regex pattern to create a named list
data = lapply(data, as_tibble) ## convert the matrices to tibbles
names(data) = paste0(names(data), ".done") ## modify the names
list2env(data, envir = .GlobalEnv) ## put them back in global environment

Read more advice on this at How do I make a list of data frames?

  • Related