【问题标题】:Mean across each element of a tibble list-column by group with purrr and dplyr使用 purrr 和 dplyr 按组跨 tibble 列表的每个元素的平均值
【发布时间】:2018-06-01 19:17:51
【问题描述】:

我正在尝试习惯使用tidyverse。我不知道我的数据是否适合使用map() 之类的函数。我喜欢列表列的组织,所以我想知道如何使用group_by()summarize()map() 和其他函数的组合来使其工作。我知道如何将这些函数与向量列一起使用,但不知道在列表列的情况下如何处理。

样本数据:

library(tidyverse)

set.seed(3949)
myList <- replicate(12, sample(1:20, size = 10), simplify = FALSE)

tibble(
  group = rep(c("A", "B"), each = 6),
  data = myList
)

列表列中的每个向量都有十个元素,这些元素是给定试验的值。我想做的是按group 对tibble 进行分组,然后找到扩展列表的“列”均值和se。换句话说,这就像我将列表列视为一个矩阵,并将 tibble 的每一行绑定在一起。输出将包含组和试验的列,因此它的格式与 ggplot2 正确。

        mean        se group trial
1   6.000000 1.6329932     A     1
2  12.666667 2.3333333     A     2
3  12.333333 2.8007935     A     3
4  13.833333 1.8150605     A     4
5   8.166667 3.1028661     A     5
6  11.500000 2.9410882     A     6
7  13.666667 2.3758040     A     7
8   6.833333 1.7779514     A     8
9  11.833333 2.3009660     A     9
10  8.666667 1.7061979     A    10
11  8.333333 1.6865481     B     1
12 12.166667 2.6002137     B     2
13 10.000000 2.7080128     B     3
14 11.833333 3.1242777     B     4
15  4.666667 1.2823589     B     5
16 12.500000 3.0413813     B     6
17  6.000000 1.5055453     B     7
18  8.166667 1.6616591     B     8
19 11.000000 2.6708301     B     9
20 13.166667 0.9457507     B    10

我通常会这样做:

set.seed(3949)

data.frame(group = rep(c("A", "B"), each = 6)) %>%
  cbind(replicate(12, sample(1:20, size = 10)) %>% t()) %>%
  split(.$group) %>%
  lapply(function(x) data.frame(mean = colMeans(x[ ,2:11]),
                                se = apply(x[ ,2:11], 2, se))) %>%
  do.call(rbind,.) %>%
  mutate(group = substr(row.names(.), 1,1),
         trial = rep(1:10, 2)) %>% 

  ggplot(aes(x = trial, y = mean)) +
  geom_point() +
  geom_line() +
  facet_grid(~ group) +
  scale_x_continuous(limits = c(1,10), breaks = seq(1, 10, 1)) +
  geom_errorbar(aes(ymin = mean-se, ymax = mean+se), color = "black") + 
  theme_bw()

tidyverse 函数有更简洁的方法吗?

【问题讨论】:

    标签: r dplyr purrr


    【解决方案1】:

    我认为另一种方法是使用nest()map()

    library(tidyverse)
    library(plotrix) #For the std.error
    
    # Your second sample dataset
    set.seed(3949)
    df <- data.frame(group = rep(c("A", "B"), each = 6)) %>%
      cbind(replicate(12, sample(1:20, size = 10)) %>% t()) 
    
    
    df %>% 
      nest(-group) %>% 
      mutate(mean = map(data, ~rowMeans(.)), 
             se = map(data, ~ plotrix::std.error(t(.))), 
             trial = map(data, ~ seq(1, nrow(.)))) %>%
      unnest(mean, se, trial) %>% 
      ggplot(aes(x = trial, y = mean)) +
      geom_point() +
      geom_line() +
      facet_grid(~ group) +
      geom_errorbar(aes(ymin = mean-se, ymax = mean+se), color = "black") + 
      theme_bw()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 2021-10-29
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多