简体   繁体   中英

R - Convert list of list into data frame

I have the next issue trying to convert this list of list into a data frame where is unique element of the list is a column of its own.

This is what I have right now:

> head(data$egg_groups)
[[1]]
 name   resource_uri
1   Plant /api/v1/egg/7/
2 Monster /api/v1/egg/1/

[[2]]
 name   resource_uri
1   Plant /api/v1/egg/7/
2 Monster /api/v1/egg/1/

[[3]]
 name   resource_uri
1   Plant /api/v1/egg/7/
2 Monster /api/v1/egg/1/

[[4]]
 name    resource_uri
1  Dragon /api/v1/egg/14/
2 Monster  /api/v1/egg/1/

[[5]]
 name    resource_uri
1  Dragon /api/v1/egg/14/
2 Monster  /api/v1/egg/1/

[[6]]
 name    resource_uri
1  Dragon /api/v1/egg/14/
2 Monster  /api/v1/egg/1/

What I would like to have is a data frame where is one of those entries (just name) is a column of its own.

Something like this:

    Plant Monster Dragon
1     1      1
2     1      1
3     1      1
4            1      1
5            1      1
6            1      1

I have tried the library plyr and the using unlist and so far nothing has worked. Any tips would be appreciated. Thanks

EDIT: This is the dput pastebin link: dput

You can use rbindlist() from data.table v1.9.5 as follows:

(Using @lukeA's example)

require(data.table) # 1.9.5+
dt = rbindlist(l, idcol="id")
#    id x y
# 1:  1 a 1
# 2:  1 b 2
# 3:  2 b 2
# 4:  2 c 3

dcast(dt, id ~ x, fun.aggregate = length)
#    id a b c
# 1:  1 1 1 0
# 2:  2 0 1 1

You can install it by following the instructions here .

I would suggest using mtabulate from the "qdapTools" package. First, just loop through the list and extract the relevant column as a vector, and use the resulting list as the input for mtabulate , something like this:

library(qdapTools)
head(mtabulate(lapply(L, `[[`, "name")))
#   Bug Ditto Dragon Fairy Flying Ground Human-like Indeterminate Mineral Monster
# 1   0     0      0     0      0      0          0             0       0       1
# 2   0     0      0     0      0      0          0             0       0       1
# 3   0     0      0     0      0      0          0             0       0       1
# 4   0     0      1     0      0      0          0             0       0       1
# 5   0     0      1     0      0      0          0             0       0       1
# 6   0     0      1     0      0      0          0             0       0       1
#   Plant Undiscovered Water1 Water2 Water3
# 1     1            0      0      0      0
# 2     1            0      0      0      0
# 3     1            0      0      0      0
# 4     0            0      0      0      0
# 5     0            0      0      0      0
# 6     0            0      0      0      0

Here's one way to do it:

(l <- list(data.frame(x = letters[1:2], y = 1:2), data.frame(x = letters[2:3], y = 2:3)))
# [[1]]
# x y
# 1 a 1
# 2 b 2
# 
# [[2]]
# x y
# 1 b 2
# 2 c 3
df <- do.call(rbind, lapply(1:length(l), function(x) cbind(l[[x]], id = x) ))
#   x y id
# 1 a 1  1
# 2 b 2  1
# 3 b 2  2
# 4 c 3  2
library(reshape2)
dcast(df, id~x, fun.aggregate = function(x) if (length(x)) "1" else "" )[-1]
#   a b c
# 1 1 1  
# 2   1 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM