I have with some data with missing values (i.e. NA values), the simplified format is below (code for input at the end):
#> id x country
#> 1 1 2.0 USA
#> 2 2 4.0 USA
#> 3 3 3.5 JPN
#> 4 4 NA JPN
For each country, I'd like to take the mean of x
, and a count of usable values of x
(i.e. not NA), so I've used group_by
, and it works for the 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
but when I try to add the count()
, I'm getting an error
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')"
My desired output would be:
#> 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
Reproducible code below:
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