【问题标题】:Taking a count() after group_by() for non-missing values在 group_by() 之后为非缺失值取一个 count()
【发布时间】:2019-10-10 06:15:29
【问题描述】:

我有一些缺失值的数据(即 NA 值),简化格式如下(最后输入代码):


#>   id   x country
#> 1  1 2.0     USA
#> 2  2 4.0     USA
#> 3  3 3.5     JPN
#> 4  4  NA     JPN

对于每个国家/地区,我想取x 的平均值和x 的可用值计数(即不是NA),所以我使用了group_by,它适用于mean:

df <- df %>% group_by(country) %>% 
  mutate(mean_x = mean(x, na.rm = TRUE),
        #count_x = count(x)) 
        )

df
#> # A tibble: 4 x 4
#> # Groups:   country [2]
#>      id     x country mean_x
#>   <dbl> <dbl> <fct>    <dbl>
#> 1     1   2   USA        3  
#> 2     2   4   USA        3  
#> 3     3   3.5 JPN        3.5
#> 4     4  NA   JPN        3.5

但是当我尝试添加 count() 时,我收到了一个错误

library(tidyverse)
df <- data.frame(id = c(1, 2, 3, 4),
                  x = c(2, 4, 3.5, NA),
                  country = c("USA", "USA", "JPN", "JPN")
                 )
df
df <- df %>% group_by(country) %>% 
  mutate(mean_x = mean(x, na.rm = TRUE),
        count_x = count(x)) 
        )

df

#> Error in UseMethod("summarise_") : no applicable method for 'summarise_' applied to an 
#> object of class "c('double', 'numeric')"

我想要的输出是:

#>      id     x country mean_x  count
#>   <dbl> <dbl> <fct>    <dbl>
#> 1     1   2   USA        3     2
#> 2     2   4   USA        3     2
#> 3     3   3.5 JPN        3.5   1
#> 4     4  NA   JPN        3.5   1

下面的可重现代码:

library(tidyverse)
df <- data.frame(id = c(1, 2, 3, 4),
                  x = c(2, 4, 3.5, NA),
                  country = c("USA", "USA", "JPN", "JPN")
                 )
df
df <- df %>% group_by(country) %>% 
  mutate(mean_x = mean(x, na.rm = TRUE),
        count_x = count(x)) 
        )

df

【问题讨论】:

  • 你可以用sum(!is.na(x))代替count

标签: r dplyr tidyverse


【解决方案1】:

count 在这里不是正确的函数。 count 的第一个参数是一个数据框或 tibble。但是,您传递的是一个向量,因此您会收到错误消息。 count 还总结了数据框,以便每组只有一行。参见例如,

library(dplyr)

df %>% 
  group_by(country) %>% 
  mutate(mean_x = mean(x, na.rm = TRUE)) %>%
  count(country)

#  country     n
#  <fct>   <int>
#1 JPN         2
#2 USA         2

如果您想添加新列而不进行汇总,请改用add_count

df %>% 
  group_by(country) %>% 
  mutate(mean_x = mean(x, na.rm = TRUE)) %>%
  add_count(country)

#     id     x country mean_x     n
#  <dbl> <dbl> <fct>    <dbl> <int>
#1     1   2   USA        3       2
#2     2   4   USA        3       2
#3     3   3.5 JPN        3.5     2
#4     4  NA   JPN        3.5     2

但是,这两个功能都不能满足您的需求。要计算每组的非 NA 值,您需要

df %>% 
  group_by(country) %>% 
  mutate(mean_x = mean(x, na.rm = TRUE), 
         count = length(na.omit(x)))
         #OR
         #count = sum(!is.na(x)))#as @Humpelstielzchen mentioned


#    id     x country mean_x count
#  <dbl> <dbl> <fct>    <dbl> <int>
#1     1   2   USA        3       2
#2     2   4   USA        3       2
#3     3   3.5 JPN        3.5     1
#4     4  NA   JPN        3.5     1

【讨论】:

    【解决方案2】:

    我们还可以使用group_by n() 创建“计数”

    library(dplyr)
    df %>% 
        group_by(country) %>% 
        mutate(mean_x = mean(x, na.rm = TRUE)) %>%
        summarise(n = n())
    # A tibble: 2 x 2
    #  country     n
    #  <fct>   <int>
    #1 JPN         2
    #2 USA         2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多