Create a separate variable for each element of a list

list, r

Solution

One way of doing it:

list2env(setNames(l,paste0("df",seq_along(l))), envir = parent.frame()) 

Though I would heed @baptiste 's advice before it is too late and avoid doing this. Why? Because in two days time you'll find yourself trapped using `get()` and `paste` and messy loops to try to apply a function to all of your datasets at once. If you keep them in a list you can just do `lapply(l,functionname)`.

Problem

I have a list of data frames, which was produced from reading in 25 .csv files at once. I would like to unlist the data frames to begin a spatial analysis. That is, I would like to have individual data frames for each element of the list. I have tried `unlist()`, but it does not produce the result I want. I have also tried an approach with `lapply()`, but it produces an error. Here's what 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 to be in assigning the data frame to the pasted name. Wrapping it with `as.character()` did not help. This does work: ``` "df1" <- data.frame(l[[1]]) ``` It seems the problem is somewhere in the output of my `paste()` function, but the output has `str()` 'chr'. Any ideas how I can make my approach work? Is there a cleaner way to unlist my data frames?

Original source