gpt4 book ai didi

r - 如何在R中将表列表转换为数据框

转载 作者:行者123 更新时间:2023-12-04 10:27:39 28 4
gpt4 key购买 nike

该问题与以下内容有关: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

最佳答案

所有其他答案都很好,我只想添加基本的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 :)

关于r - 如何在R中将表列表转换为数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com