【问题标题】:Use tidyverse to replace NA with mean of data, by group [duplicate]使用 tidyverse 按组用数据的平均值替换 NA [重复]
【发布时间】:2019-10-28 13:46:18
【问题描述】:

我正在尝试编写一个函数,将数值 data.frame 列中的 NA 替换为该变量存在的数据的平均值(按组)。我意识到这是估算,并且有相应的软件包,我更愿意自己做,平均值只是一个例子,将使用更复杂的函数。我试图制作一个 mwe,但我在接近尾声时被卡住了。我正在尝试,尽可能坚持使用 tidyverse 方法。

library(tidyverse)
## First create a little dataset for a minimum working example for questions
## three vectors
id <- c(rep("boh1", 6), rep("boh2", 6), rep("boh3", 6), rep("boh4", 6))
operator <- rep(c("op1", "op2"), each = 12)
nummos <- c(1, 4, 4, 3, 1, NA, 4, 2, 2, 3, 4, 4, NA, 1, 1, 5,
                     5, 4, 5, 3, 2, NA, 3, 3)
## combine vectors into df
dat1 <- data.frame(id, operator, nummos)
## group by two variables and get mean of variable by group
dat2 <- dat1 %>%
    group_by(id, operator) %>%
    summarize(mean = mean(nummos, na.rm=TRUE))
## now stuck, how to replace NA by mean value appropriate for that group?

【问题讨论】:

    标签: r replace dplyr na


    【解决方案1】:

    使用mutate 和dplyr::case_when 代替summarise:

    dat1 %>%
        group_by(id, operator) %>%
        mutate(nummos2 = case_when(is.na(nummos) ~ mean(nummos, na.rm=TRUE),
                                   TRUE ~ as.numeric(nummos) 
                                  )
               )
    

    【讨论】:

    • 如果您只测试 1 个条件,case_when 与基本 ifelse 相比有什么好处?
    • 这是真的,我相信两者都具有相同的性能。可以留在tidyverse 或在这里使用base。
    • 看起来工作得很好,谢谢。
    • 当然,但是仅仅因为您使用了其他dplyr 函数,这并不意味着您不应该也使用基函数,尤其是在基函数更简单或更合适的情况下(例如,用于检查 1 个条件,而不是多个条件,这是 case_when 的设计目的)
    • @JimMaas 你要我重新提出问题?我不能。您可以查看help section 以供重复参考。将问题作为重复关闭不一定是一件坏事。
    【解决方案2】:

    我对 tidyverse 不是很熟悉,所以这里有一个data.table 解决方案:

    library(data.table) # load package
    setDT(dat1) # convert data.frame to data.table
    

    现在,我将通过c(id, operator) 创建一个平均值为nummos 的data.table,然后将其合并 与dat1,用计算值填充NAs:

    dat1[dat1[, mean(nummos, na.rm = TRUE), by = .(id, operator)], nummos := ifelse(is.na(nummos), i.V1, nummos), on = .(id, operator)]
    

    dat1[, mean(nummos, na.rm = TRUE), by = .(id, operator)] 是一个小型 data.table,其中包含按组的方式。

    nummos := ifelse... 部分仅在 nummos 为 NA 时才进行赋值。

    dat1
          id operator nummos
     1: boh1      op1    1.0
     2: boh1      op1    4.0
     3: boh1      op1    4.0
     4: boh1      op1    3.0
     5: boh1      op1    1.0
     6: boh1      op1    2.6
     7: boh2      op1    4.0
     8: boh2      op1    2.0
     9: boh2      op1    2.0
    10: boh2      op1    3.0
    11: boh2      op1    4.0
    12: boh2      op1    4.0
    13: boh3      op2    3.2
    14: boh3      op2    1.0
    15: boh3      op2    1.0
    16: boh3      op2    5.0
    17: boh3      op2    5.0
    18: boh3      op2    4.0
    19: boh4      op2    5.0
    20: boh4      op2    3.0
    21: boh4      op2    2.0
    22: boh4      op2    3.2
    23: boh4      op2    3.0
    24: boh4      op2    3.0
      id operator nummos
    

    【讨论】:

      【解决方案3】:

      您可以使用replace() 简单地定义自己的函数。试试看:

      dat1 %>% 
              group_by(id, operator) %>% 
              mutate_at("nummos", function(x) replace(x, is.na(x), mean(x, na.rm = TRUE)))
      # output
      # A tibble: 24 x 3
      # Groups:   id, operator [4]
         id    operator nummos
         <fct> <fct>     <dbl>
       1 boh1  op1         1  
       2 boh1  op1         4  
       3 boh1  op1         4  
       4 boh1  op1         3  
       5 boh1  op1         1  
       6 boh1  op1         2.6
       7 boh2  op1         4  
       8 boh2  op1         2  
       9 boh2  op1         2  
      10 boh2  op1         3  
      # ... with 14 more rows
      

      【讨论】:

        【解决方案4】:

        的另一个解决方案,带有(相当新的)nafill-function:

        library(data.table)
        setDT(dat1)
        
        dat1[, nummos := nafill(nummos, "const", fill = mean(nummos, na.rm = TRUE))
             , by = .(id, operator)]
        

        还有一个使用na.aggregate 来自-package 的 解决方案:

        dat1 %>%
          group_by(id, operator) %>%
          mutate_at("nummos", zoo::na.aggregate)
        

        【讨论】:

        • Jaap,这个函数怎么知道我想用平均值替换?谢谢
        • @JimMaas 默认是平均值;如果你想使用另一个函数,你应该指定它;另请参阅帮助文件:?zoo::na.aggregate
        • Jaap,谢谢你,这很有帮助。能否请您删除上面表明它已经回答过的cmets?我找到了这两个答案,但以我的中级能力,无法制定自愿提出的四个非常好的解决方案中的任何一个。如果我们都已经是所有方面的专家,我们就不会在这里提问。很高兴讨论,谢谢,J。
        • @JimMaas 我可以,但恕我直言,这是一个正确的副本。
        猜你喜欢
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 2020-06-17
        • 2012-03-08
        • 1970-01-01
        • 2016-10-03
        • 2019-11-26
        相关资源
        最近更新 更多