r - Create a separate variable for each element of a list -
i have list of data frames, produced reading in 25 .csv files @ once. unlist data frames begin spatial analysis. is, have individual data frames each element of list.
i have tried unlist()
, not produce result want. have tried approach lapply()
, produces error. here's i've tried:
x <- 1:3 y <- 4:6 l <- lapply(1:2, function(x){data.frame(x, y)}) lapply(1:length(l), function(i){paste('df', i, sep = '') <- data.frame(l[[i]])})
the problem seems in assigning data frame pasted name. wrapping as.character()
did not help.
this work:
"df1" <- data.frame(l[[1]])
it seems problem somewhere in output of paste()
function, output has str()
'chr'. ideas how can make approach work? there cleaner way unlist data frames?
one way of doing it:
list2env(setnames(l,paste0("df",seq_along(l))), envir = parent.frame())
though heed @baptiste 's advice before late , avoid doing this. why? because in 2 days time you'll find trapped using get()
, paste
, messy loops try apply function of datasets @ once. if keep them in list can lapply(l,functionname)
.
Comments
Post a Comment