如何将表列表转换为R中的数据框

时间:2014-10-13 12:43:34

标签: r

此问题与:Count values in the columns separated by ":"

有关

我有以下表格列表:

ll = structure(list(`001` = structure(c(1L, 2L, 1L, 1L), .Dim = 4L, .Dimnames = structure(list(
    c("Active", "Com.Tent", "Perform", "Sport_Well")), .Names = ""), class = "table"), 
    `002` = structure(c(1L, 2L, 5L, 2L), .Dim = 4L, .Dimnames = structure(list(
        c("Active", "Com.Tent", "Perform", "Sport_Well")), .Names = ""), class = "table"), 
    `003` = structure(c(2L, 1L, 4L), .Dim = 3L, .Dimnames = structure(list(
        c("Active", "Com.Tent", "Perform")), .Names = ""), class = "table")), .Names = c("001", 
"002", "003"))

ll

$`001`

    Active   Com.Tent    Perform Sport_Well 
         1          2          1          1 

$`002`

    Active   Com.Tent    Perform Sport_Well 
         1          2          5          2 

$`003`

  Active Com.Tent  Perform 
       2        1        4 

如何将其转换为以下数据框:

user_id  Active Com.tent Perform  Sport_Well
001     1       2       1       1
002     1       2       5       2
003     2       1       4       0

6 个答案:

答案 0 :(得分:4)

来自rbindlist包的

data.table有一个fill参数,可以处理rbind ed列表中不同的列:

library(data.table)
tmp <- rbindlist(lapply(ll, function(x) as.data.frame.list(x)), fill=TRUE)
tmp$user_id <- names(ll)

##    Active Com.Tent Perform Sport_Well user_id
## 1:      1        2       1          1     001
## 2:      1        2       5          2     002
## 3:      2        1       4         NA     003

答案 1 :(得分:3)

这是一种非常手动的方法(但通常很有效):

## Get the unique column names needed
colNames <- unique(unlist(lapply(ll, names)))

## Create an empty matrix to hold the data
M <- matrix(0, nrow = length(ll), ncol = length(colNames), 
            dimnames = list(names(ll), colNames))

## Match the matrix column names with the required values
matches <- lapply(ll, function(x) match(names(x), colNames))

## Use matrix indexing to replace the required values
M[cbind(rep(sequence(nrow(M)), sapply(matches, length)),
        unlist(matches))] <- unlist(ll)
M
#     Active Com.Tent Perform Sport_Well
# 001      1        2       1          1
# 002      1        2       5          2
# 003      2        1       4          0

结果是matrix,因此如果您想要data.frame,则需要as.data.frame

答案 2 :(得分:2)

所有其他答案都运行得很好,我只想添加基础R解决方案

max.length  <- max(sapply(ll, length))
ll <- lapply(ll, function(x) {length(x) <- max.length; x})
d <- data.frame(do.call(rbind, ll))
d$user_id <- rownames(d)

如果你想用你的样本输出中的零替换NA,d[is.na(d)] <- 0就像你自己建议的那样:)

答案 3 :(得分:2)

我找到了一个简单的方法:

> library(reshape2)
> dcast(melt(ll), L1~Var1)
   L1 Active Com.Tent Perform Sport_Well
1 001      1        2       1          1
2 002      1        2       5          2
3 003      2        1       4         NA

答案 4 :(得分:1)

您还可以使用unnest

中的tidyr
 devtools::install_github("hadley/tidyr")
 library(tidyr)
 unnest(lapply(ll, as.data.frame.list), user_id)
 #  user_id Active Com.Tent Perform Sport_Well
 #1     001      1        2       1          1
 #2     002      1        2       5          2
 #3     003      2        1       4         NA

答案 5 :(得分:1)

dplyr::rbind_all也有效。

library(dplyr)    
cbind(user_id = names(ll), rbind_all(lapply(ll, as.data.frame.list)))
#   user_id Active Com.Tent Perform Sport_Well
# 1     001      1        2       1          1
# 2     002      1        2       5          2
# 3     003      2        1       4         NA