gpt4 book ai didi

r - 展平数据框

转载 作者:行者123 更新时间:2023-12-04 00:42:40 26 4
gpt4 key购买 nike

我有这个嵌套的数据框

test <- structure(list(id = c(13, 27), seq = structure(list(
`1` = c("1997", "1997", "1997", "2007"),
`2` = c("2007", "2007", "2007", "2007", "2007", "2007", "2007")),
.Names = c("1", "2"))), .Names = c("penr",
"seq"), row.names = c("1", "2"), class = "data.frame")

我想要第二列中所有值的列表,即
result <- c("1997", "1997", "1997", "2007", "2007", "2007", "2007", "2007", "2007", "2007", "2007")

有没有简单的方法来实现这一目标?

最佳答案

这一行可以解决问题:

do.call("c", test[["seq"]])

或等效的:
c(test[["seq"]], recursive = TRUE)

甚至:
unlist(test[["seq"]])

这些函数的输出是:
    11     12     13     14     21     22     23     24     25     26     27 
"1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007" "2007"

要删除字符向量上方的名称,请调用 as.character在结果对象上:
> as.character((unlist(test[["seq"]])))
[1] "1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007"
[11] "2007"

关于r - 展平数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9467321/

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