为什么tidyr这样筑巢?

时间:2016-01-21 20:51:10

标签: r tidyr

我正在使用nest(版本0.4.0)中的tidyr函数将变量嵌套在一个简单的数据框中:

df <- structure(list(id = 1:4, type = c("B", "A", "B", "B")),
                class = "data.frame", row.names = c(NA, -4L),
                .Names = c("id", "type"))
df
#   id type
# 1  1    B
# 2  2    A
# 3  3    B
# 4  4    B

tidyr::nest(df, id)
#   type    data
# 1    B       2
# 2    A 1, 3, 4

为什么嵌套数据框的第一行没有type = "A", data = 2?这是一个错误还是我只是误解nest应该做什么?

1 个答案:

答案 0 :(得分:1)

tidyr 0.3.1为我生成正确的输出:

library(tidyr)

df <- structure(list(id = 1:4, type = c("B", "A", "B", "B")),
                class = "data.frame", row.names = c(NA, -4L),
                .Names = c("id", "type"))

df2 <- nest(df, id)
as.data.frame(df2)
#   type      id
# 1    A       2
# 2    B 1, 3, 4

sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1