1
votes

I'm trying to replicate an 'old' R script I found for the tidyverse package.

library(dslabs)
DataTib<-as_tibble(us_contagious_diseases)


DataTib_nested <- DataTib %>%
  group_by(disease) %>%
  nest()

Mean_count_nested <- DataTib_nested %>%
  mutate(mean_count = map(.x=DataTib_nested$data, ~mean(.x$count)))

As I understand, I have a tibble where data was grouped by disease and the remaining variables/data were nested, and then I'm trying to add a new column which should represent the average for variable "count" on that nested dataframe.

But I get the error, which I don't quite understand:

Error: Problem with `mutate()` input `mean_count`.
x Input `mean_count` can't be recycled to size 1.
i Input `mean_count` is `map(.x = DataTib_nested$data, ~mean(.x$count))`.
i Input `mean_count` must be size 1, not 7.
i The error occured in group 1: disease = "Hepatitis A".

Thanks in advance and best regards!

1
Please add data using dput and show the expected output for the same. Please read the info about how to ask a good question and how to give a reproducible example. - Ronak Shah
Do you need to nest the data? If you just want average count for each disease you can do DataTib %>% group_by(disease) %>% summarise(mean_count = mean(count, na.rm = TRUE)) - Ronak Shah

1 Answers

1
votes

Your syntax is slightly wrong:

DataTib_nested <- DataTib %>% 
  group_by(disease) %>% 
  nest(data = - disease) 

Mean_count_nested <- DataTib_nested %>% 
  mutate(mean_count = map_dbl(data, ~mean(.x$count)))

Note that I use map_dbl instead of map since the return value is numeric.