Suppose we have a data frame like this:
df <- data.frame(a = 1:3)
df$b <- lapply(1:3, function(x) list(sample(3)))
df
#> a b
#> 1 1 1, 2, 3
#> 2 2 3, 1, 2
#> 3 3 2, 3, 1
Here we can see that column b
is a list of lists:
class(df$b)
#> [1] "list"
class(df$b[1])
#> [1] "list"
If we want to make each list in each row of column b a data frame, we can do:
df_list <- setNames(lapply(df$b, function(x) data.frame(var = x[[1]])),
c("df1", "df2", "df3"))
This gives us a list of data frames, each with its own name:
df_list
#> $df1
#> var
#> 1 1
#> 2 2
#> 3 3
#>
#> $df2
#> var
#> 1 3
#> 2 1
#> 3 2
#>
#> $df3
#> var
#> 1 2
#> 2 3
#> 3 1
If we want, we can extract these into the global workspace like this:
list2env(df_list, globalenv())
So now we have
df1
#> var
#> 1 2
#> 2 3
#> 3 1
df2
#> var
#> 1 1
#> 2 2
#> 3 3
df3
#> var
#> 1 3
#> 2 1
#> 3 2
Personally, for most uses I would keep the data frames in df_list
to avoid polluting the global workspace and make it easier to iterate through the data frames.