0

My Data Frame looks like this:

list1
list2
list3
.
.
.
list30

Within each list are vector data. I want each list to be a dataframe. I don't know how to do this.


Edit: @Allen Cameron already answered the question.

To get dataframe with lists to each list within the dataframe becoming dataframes use:

list2env(df_list, globalenv())

where

df_list 

is the original dataframe with the lists

Brian D
  • 79
  • 7
  • 3
    Please use `dput` to post something a bit more specific. – Michael Dewar Nov 12 '20 at 14:16
  • To the best of my knowledge, it is not possible to store a list within a data frame. – MacOS Nov 12 '20 at 14:22
  • @MacOS it is possible to have a column of a data frame that is a list. For example, `dplyr::nest` will put a list of data frames as a column of a data frame. – Michael Dewar Nov 12 '20 at 14:26
  • @MichaelDewar You are right. But this is a different case because his example seems to imply that one entry (row, column) in a data frame can be list. Which is not possible. I'm unsure what he really wants/deals with exactly. – MacOS Nov 12 '20 at 14:32
  • @MacOS that's not true either. Each entry in the column can be a list. – Allan Cameron Nov 12 '20 at 14:35
  • @AllanCameron I just tried to save a list in a data frame, and it does not work. Can you please provide a short code snippet? Thank you. – MacOS Nov 12 '20 at 14:38
  • @MacOS see my answer for a reprex – Allan Cameron Nov 12 '20 at 14:39
  • 1
    @AllanCameron I was clearly wrong. Thank you for the example. – MacOS Nov 12 '20 at 14:41
  • I want to split up dataframe. Each list I want as a new dataframe. something like this: https://stackoverflow.com/questions/13125846/split-dataframe-in-r-by-row , but for each list instead of (a,b,c) in one dataframe and (d) in the other dataframe. I want nth number of splits in the dataframe depending on the number of lists. Thanks. Sorry I couldn't explain better earlier. – Brian D Nov 12 '20 at 15:16
  • @BrianD Please post the output of `dput(head(df))` where `df` should be replaced by the name of your data frame. – Michael Dewar Nov 12 '20 at 15:38

2 Answers2

4

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.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • list2env(df_list, globalenv()), where df_list is the original dataframe with the lists. Thank you, that's all I wanted to know. This would be useful to anyone who wants to know it. – Brian D Nov 13 '20 at 17:35
  • @BrianD you're welcome. If this answer has helped please consider marking it as accepted. Thank you. – Allan Cameron Nov 13 '20 at 17:54
0

I can't post code, slackoverflow won't allow it.

I have 37 lists in dataframe 'file contents'. I want each list to be an induvidual dataframe

Brian D
  • 79
  • 7
  • You already have a list of data frames. If you want to access them, you can try `file_contents[[7]]` to access the 7th one, for example. Was @Allen Cameron's answer not what you were looking for? – Michael Dewar Nov 12 '20 at 19:26