r - 在 group_by() 之后获取 count() 以获取非缺失值

标签 r dplyr tidyverse

我有一些缺失值(即 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

最佳答案

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

关于r - 在 group_by() 之后获取 count() 以获取非缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58316539/

相关文章:

r - 连接组中可变数量的行以创建单行并最好在 dplyr 中从长格式转换为宽格式

r - 安装胶水后 tidyverse 的问题

r - 如何对字符串向量中的每个元素进行子字符串化?

r - 如何使用 R 中的 rjson/RJSONIO 的 toJSON 方法生成有效的 JSON?

r - 如何查看由 ggplot2 geom_boxplot 计算的计算变量?

r - R中的合并和交织数据帧

r - 如何在其他列中查找唯一值相对分配ID

r - 组不互斥时类似于 group_by 的功能

r - 完成前一行中的计算后填充变量实例

r - 如何在 tidyverse 中将缺失的数据行添加为 NA