List Elements to Dataframes in R

Convert a list to a data frame

Update July 2020:

The default for the parameter stringsAsFactors is now default.stringsAsFactors() which in turn yields FALSE as its default.


Assuming your list of lists is called l:

df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=TRUE))

The above will convert all character columns to factors, to avoid this you can add a parameter to the data.frame() call:

df <- data.frame(matrix(unlist(l), nrow=132, byrow=TRUE),stringsAsFactors=FALSE)

Storing a list within a data frame element in R

The option mentioned in my comment, i.e. simply using a list for one of the columns:

dat <- data.frame(Start = 3:4, End = c(6,10))
> dat
Start End
1 3 6
2 4 10
> dat$Elements <- list(4:5,7:9)
> dat
Start End Elements
1 3 6 4, 5
2 4 10 7, 8, 9

You could also of course ditch data frames entirely and simply use a plain old list (which might make more sense in a lot of cases, anyway):

list(list(Start = 3,End = 6, Elements = 4:5),list(Start = 4,End = 10,Elements = 7:9))
[[1]]
[[1]]$Start
[1] 3

[[1]]$End
[1] 6

[[1]]$Elements
[1] 4 5

[[2]]
[[2]]$Start
[1] 4

[[2]]$End
[1] 10

[[2]]$Elements
[1] 7 8 9

List elements to dataframes in R

If you want to convert your list elements to data.frames, you can try either

lapply(exlist, as.data.frame)

Or (as suggested by @Richard), depends on your desired output:

lapply(exlist, as.data.frame.list)

It is always recommended to keep multiple data frames in a list rather than polluting your global environment, but if you insist on doing this, you could use list2env (don't do this), such as:

list2env(lapply(exlist, as.data.frame.list), .GlobalEnv)

How do I make a list of data frames?

This isn't related to your question, but you want to use = and not <- within the function call. If you use <-, you'll end up creating variables y1 and y2 in whatever environment you're working in:

d1 <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))
y1
# [1] 1 2 3
y2
# [1] 4 5 6

This won't have the seemingly desired effect of creating column names in the data frame:

d1
# y1....c.1..2..3. y2....c.4..5..6.
# 1 1 4
# 2 2 5
# 3 3 6

The = operator, on the other hand, will associate your vectors with arguments to data.frame.

As for your question, making a list of data frames is easy:

d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6))
d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))
my.list <- list(d1, d2)

You access the data frames just like you would access any other list element:

my.list[[1]]
# y1 y2
# 1 1 4
# 2 2 5
# 3 3 6

Converting the names of a list elements as a variable in a data.frame

When I think of iterating over a vector I think of the map functions in purrr. In this case, map2() iterates over multiple arguments simultaneously. I grabbed the names of the list elements and stored them in a vector, n:

n <- names(List)

I then iterated over List and n at the same time, calling cbind() to add a variable (i.e., column) to each data frame:

map2(List, n, ~ cbind(.x, Name = .y))

Output:


$bar1
study sd Name
1 A 1 bar1

$bar2
study sd Name
1 B 2 bar2
2 C 3 bar2

$bar3
study sd Name
1 Z 4 bar3

$bar4
study sd Name
1 H 5 bar4

We can collapse List to a single data frame with a call to bind_rows():

result <- map2(List, n, ~ cbind(.x, Name = .y))
bind_rows(result)

Output:


study sd Name
1 A 1 bar1
2 B 2 bar2
3 C 3 bar2
4 Z 4 bar3
5 H 5 bar4

Taken as one statement that would be:

map2(List, names(List), ~ cbind(.x, Name = .y)) %>% 
bind_rows()

Converting list with elements of different sizes into data frame in R

We can use map with enframe to return a tibble with the first column the name of the list element and the second column a list

library(dplyr)
library(tibble)
library(purrr)
library(stringr)
map_dfr(lst1, enframe)

If we need to paste the list elements together

map_dfr(lst1, ~ enframe(.x) %>%
mutate(value = map_chr(value, str_c, collapse=", ")))

-output

# A tibble: 2 x 2
# name value
# <chr> <chr>
#1 LISTNAME1 item1, item2, item3, item4
#2 LISTNAME2 item2, item3, item32109

data

lst1 <- list(list(LISTNAME1 = c("item1", "item2", "item3", "item4")), 
list(LISTNAME2 = c("item2", "item3", "item32109")))

Convert list of list object to dataframe in R

Something like (ls is your list):

df <- data.frame(matrix(unlist(ls), ncol = max(lengths(ls)), byrow = TRUE))

If column names matter, then

names(df) <- names(ls[[which(lengths(ls)>0)[1]]])

How to create a list of elements in columns based on another in R dataframe

This solution using dplyr returns lists for the values (not sure if you wanted character strings or lists but the brackets in your example led me to think you wanted lists...)

library(dplyr)
df1 %>%
group_by(col1) %>%
summarise(across(, list))

How to combine all the elements of list to a dataframe in R

You can easily combine the data.frames with

do.call(rbind, mylist)


Related Topics



Leave a reply



Submit