【问题标题】:Compute mean by groups working with lists in R通过在 R 中使用列表的组计算平均值
【发布时间】:2021-11-30 02:25:49
【问题描述】:

我想:

  1. 在每个列表中使用以下变量(idmonth)进行分组。然后,我想平均感兴趣变量的值 (preds)
id <- c(1,2,3,4,5,1,2,3,4,5)
month <- c(3,4,2,1,5,7,3,1,8,9)
preds <- c(0.5,0.1,0.15,0.23,0.75,0.6,0.49,0.81,0.37,0.14)

l_1 <- data.frame(id, preds, month)

preds <- c(0.45,0.18,0.35,0.63,0.25,0.63,0.29,0.11,0.17,0.24)

l_2 <- data.frame(id, preds, month)

preds <- c(0.58,0.13,0.55,0.13,0.76,0.3,0.29,0.81,0.27,0.04)

l_3 <- data.frame(id, preds, month)

preds <- c(0.3,0.61,0.18,0.29,0.85,0.76,0.56,0.91,0.48,0.91)

l_4 <- data.frame(id, preds, month)

outcome <- list(l_1, l_2, l_3, l_4)

我尝试使用地图功能和 sapply 没有任何成功。 有什么帮助吗?

提前谢谢你

【问题讨论】:

    标签: r list group-by grouping mean


    【解决方案1】:

    使用lapply()aggregate()

    res1 <- lapply(
      outcome, 
      function(x){
        aggregate(
          preds ~ id + month,
          data = x,
          FUN = mean
        )[,names(x)]
      }
    )
    

    【讨论】:

    • 谢谢@hello_friend。请注意,您的代码不会创建 outcome2。结果 2 中的每个列表每个变量都有 8 个观察值,而您的代码给出了原始数字 (10),因此它没有汇总信息以创建平均值
    • 我不知道你的结果是什么,但这段代码绝对是,对于每个列表元素,按 id 和月份分组并生成 preds 的平均值。
    【解决方案2】:

    使用tidyverse -

    library(dplyr)
    library(purrr)
    
    map(outcome, ~.x %>% 
                   group_by(id, month) %>% 
                   summarise(preds = mean(preds), .groups = 'drop'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2012-06-03
      • 1970-01-01
      • 2020-08-17
      相关资源
      最近更新 更多