代码之家  ›  专栏  ›  技术社区  ›  J.Sabree

在R中,使用nest后,如何变异以将平均值作为列输入?

  •  1
  • J.Sabree  · 技术社区  · 3 年前

    我正在尝试学习如何使用nest(),并尝试按参与者可能所在的3个时间段中的一个时间段进行嵌套,我想添加两列。第一列是总体平均值,我已经算出了。然后,我想通过时间变量嵌套,创建3个数据集(我已经计算出了),然后计算组平均值。我读到你应该创建一个函数( here ,第6.3.1节),但我的功能一直失败。我该怎么做?

    此外,请在解决方案中使用nest或nest_by。我知道我可以像其他人一样使用group_by() here ,但在我的实际数据中,由于我需要进行其他计算,我需要这些数据集是3个独立的数据集。

    #Here's my setup and sample data
    library(dplyr)
    library(purrr)
    library(tidyr)
    
    set.seed(1414)
    test <- tibble(id = c(1:100),
                   condition = c(rep(c("pre", "post"), 50)),
                   time = c(case_when(condition == "pre" ~ 0,
                                      condition == "post" ~ sample(c(1, 2), size = c(100), replace = TRUE))),
                   score = case_when(time == 0 ~ 1,
                                     time == 1 ~ 10,
                                     time == 2 ~ 100))
    
    
    #Here's what I tried
    
    #Nesting the data (works)
    nested_test <- test %>%
      unite(col = "all_combos", c(condition, time)) %>%
      mutate(score2 = mean(score)) %>%
      nest_by(all_combos)
    
    #Make mean function and map it (doesn't work)
    
    my_mean <- function(data) {
      mean(score, na.rm = T)
    }
    
    nested_test %>%
      mutate(score3 = map(data, my_mean))
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   akrun    3 年前

    我们可能需要 ungroup 因为有 rowwise 属性,然后在上循环 data 具有 map 并使用创建列 mutate 关于嵌套 数据

    library(dplyr)
    library(purrr)
    nested_test_new <- nested_test %>%
      ungroup %>%
       mutate(data = map(data, ~ .x %>%
        mutate(score3 = mean(score, na.rm = TRUE))))
    

    -输出

    nested_test_new
    # A tibble: 3 × 2
      all_combos data             
      <chr>      <list>           
    1 post_1     <tibble [19 × 4]>
    2 post_2     <tibble [31 × 4]>
    3 pre_0      <tibble [50 × 4]>
    > nested_test_new$data
    [[1]]
    # A tibble: 19 × 4
          id score score2 score3
       <int> <dbl>  <dbl>  <dbl>
     1     2    10   33.4     10
     2     4    10   33.4     10
     3    14    10   33.4     10
     4    16    10   33.4     10
     5    18    10   33.4     10
     6    28    10   33.4     10
     7    30    10   33.4     10
     8    32    10   33.4     10
     9    38    10   33.4     10
    10    44    10   33.4     10
    11    48    10   33.4     10
    12    60    10   33.4     10
    13    64    10   33.4     10
    14    78    10   33.4     10
    15    80    10   33.4     10
    16    86    10   33.4     10
    17    92    10   33.4     10
    18    96    10   33.4     10
    19   100    10   33.4     10
    
    [[2]]
    # A tibble: 31 × 4
          id score score2 score3
       <int> <dbl>  <dbl>  <dbl>
     1     6   100   33.4    100
     2     8   100   33.4    100
     3    10   100   33.4    100
     4    12   100   33.4    100
    ...
    

    或者另一种选择是 nest_mutate 从…起 nplyr

    library(nplyr)
    test %>%
      unite(col = "all_combos", c(condition, time)) %>%
      mutate(score2 = mean(score)) %>%
      nest(data = -all_combos) %>%
      nest_mutate(data, score3 = mean(score, na.rm = TRUE))
    

    -输出

    # A tibble: 3 × 2
      all_combos data             
      <chr>      <list>           
    1 pre_0      <tibble [50 × 4]>
    2 post_1     <tibble [19 × 4]>
    3 post_2     <tibble [31 × 4]>