Search code examples
rdplyrgroup-bysummarize

Error in dplyr group_by %>% summarize_if()


I am working a fairly small dataset, attempting to summarize the columns by mean, while grouping by the first column. Currently I have a df (LitterMean) that looks as such:

      date3 TotalBorn LiveBorn StillBorn Mummies
1   7/6        12       12         0       0
2   7/6        20       15         2       3
3  6/29        14       14         0       0
4   7/6        11       10         1       0
5   7/6        16       15         1       0
6   7/6        11       11         0       0

I attempt to run

LitterMean %>%
  group_by(date3) %>%
  summarize_if(LitterMean, is.numeric, mean, na.rm=TRUE)

Which returns

Error: `.predicate` must have length 1, not 5.
Run `rlang::last_error()` to see where the error occurred.

So I run the rlang::last_error() and recieve

<error/rlang_error>
`.predicate` must have length 1, not 5.
Backtrace:
  1. `%>%`(...)
  2. dplyr::summarize_if(., LitterMean, is.numeric, mean, na.rm = TRUE)
  3. dplyr:::manip_if(...)
  4. dplyr:::tbl_if_syms(.tbl, .predicate, .env, .include_group_vars = .include_group_vars)
  8. dplyr:::tbl_if_vars(.tbl, .p, .env, ..., .include_group_vars = .include_group_vars)
  9. dplyr:::bad_args(".predicate", "must have length 1, not {length(.p)}.")
 10. dplyr:::glubort(fmt_args(args), ..., .envir = .envir)
Run `rlang::last_trace()` to see the full context.

The following shows I do have NA articles.

sum(is.na(LitterMean))
[1] 5

Does anyone know if I am missing anything in my code that would prevent the above error?


Solution

  • You only need to call summarize_if properly, like this:

    LitterMean %>%
      group_by(date3) %>%
      summarize_if(is.numeric, mean, na.rm=TRUE)
    

    Expected result:

    > LitterMean %>%
    +   group_by(date3) %>%
    +   summarize_if(is.numeric, mean, na.rm=TRUE)
    # A tibble: 2 × 5
      date3 TotalBorn LiveBorn StillBorn Mummies
      <chr>     <dbl>    <dbl>     <dbl>   <dbl>
    1 6/29         14     14         0       0  
    2 7/6          14     12.6       0.8     0.6