【问题标题】:Access all the columns with a particular name in nested lists in R访问 R 中嵌套列表中具有特定名称的所有列
【发布时间】:2020-03-30 21:08:19
【问题描述】:

我想知道如何访问嵌套列表中具有特定名称的所有列。下面有一个可重现的例子。如何调用所有“平均”列并将所有列整理到单个 data.frame 中,其中 data.frame 作为另外两个指定关联类和 Output1/Output2 的列(示例 1)。示例 2 稍微复杂一点,其中嵌套的“均值”列表是一个 data.frame。我需要同时访问“ts”和“value”列。也就是说,我需要知道每个值对应的ts(除了classes和Output1/Output2)。

示例 1

classes <- c("F", "G", "M", "O")
classes <- structure(unique(classes), names = unique(classes))

S1 = data.frame(X1 = rnorm(100), X2 = rnorm(100), X3 = rnorm(100),  X4 = rep(classes, 25))
S2 = data.frame(X1 = rnorm(100), X2 = rnorm(100), X3 = rnorm(100),  X4 = rep(classes, 25))

P <- lapply(classes, function(c){
  Output1 <- list ("model" = lm(X3~ X1+X2, data = S1),"mean" = apply(S1[S1$X4 == c, 1:3], 2, mean), "sum" = apply(S1[S1$X4 == c, 1:3], 2, sum))
  Output2 <- list ("model" = lm(X3~ X1+X2, data = S2), "mean" = apply(S2[S2$X4 == c, 1:3], 2, mean), "sum" = apply(S2[S2$X4 == c, 1:3], 2, sum))
  output <- list ("Output1" = Output1, "Output2" = Output2)
  return(output)
})

示例 2

classes <- c("F", "G", "M", "O")
classes <- structure(unique(classes), names = unique(classes))

S1 = data.frame( X1 = rnorm(100), X2 = rnorm(100), X3 = rnorm(100),  X4 = rep(classes, 25), ts = seq(from = ISOdate(1910,1,1),  by = "30 min", length.out = 100 ))
S2 = data.frame( X1 = rnorm(100), X2 = rnorm(100), X3 = rnorm(100),  X4 = rep(classes, 25),ts = seq(from = ISOdate(1910,1,1),  by = "30 min", length.out = 100 ))

P <- lapply(classes, function(c){
  Output1 <- list ("model" = lm(X3~ X1+X2, data = S1),"mean" = data.frame(ts = S1[S1$X4 == c, "ts"], 
                                                                          value = S1[S1$X4 == c, "X1"]) ,
                   "sum" = apply(S1[S1$X4 == c, 1:3], 2, sum))
  Output2 <- list ("model" = lm(X3~ X1+X2, data = S2),
                   "mean" = data.frame(ts = S2[S2$X4 == c, "ts"],
                                       value = S2[S2$X4 == c, "X1"]), 
                   "sum" = apply(S2[S2$X4 == c, 1:3], 2, sum))
  output <- list ("Output1" = Output1, "Output2" = Output2)
  return(output)
})

【问题讨论】:

    标签: r dataframe nested


    【解决方案1】:

    我们可以使用rvestpluckP 获取“平均”列,并将它们与map_df 绑定在一起。

    purrr::map_df(P, ~rvest::pluck(.x, "mean"), .id = "Class")
    
    # A tibble: 12 x 3
    #   Class Output1 Output2
    #   <chr>   <dbl>   <dbl>
    # 1 F      0.0315 -0.0946
    # 2 F      0.0935  0.219 
    # 3 F      0.155   0.172 
    # 4 G      0.123   0.182 
    # 5 G     -0.114  -0.128 
    # 6 G     -0.0654 -0.0990
    # 7 M      0.111   0.0794
    # 8 M     -0.176   0.405 
    # 9 M      0.265  -0.0747
    #10 O      0.0207 -0.250 
    #11 O     -0.0407  0.0117
    #12 O     -0.162  -0.195 
    

    在base R中,你可以这样做:

    temp <- lapply(P, function(x) sapply(x, `[[`, "mean"))
    do.call(rbind, Map(cbind.data.frame, temp, Class = names(temp)))
    

    编辑

    对于数据框示例,我们可以在pluck 之后使用bind_rows

    map_df(P, ~rvest::pluck(.x, "mean") %>% bind_rows(.id= "output"), .id = "Class")
    

    【讨论】:

    • 谢谢。如果我要访问的嵌套列表只有一个列,则上述解决方案有效,但如果那是一个 data.frame 怎么办?我编辑了问题并提供了第二个示例。
    • @Ronak 这不会将 Output1/Output2 向前传送。我将其更改为 purrr::map_df(P, ~rvest::pluck(.x, "mean") %>% bind_cols, .id = "Class")
    • @Nile 对不起,我错过了,我们可以使用map_df(P, ~rvest::pluck(.x, "mean") %&gt;% bind_rows(.id= "output"), .id = "Class")。更新了答案。
    • 还有其他人收到以下错误Error: 'pluck' is not an exported object from 'namespace:rvest'
    • 是的,pluck 功能似乎已从 rvest 中删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多