Search code examples
rdplyrtidyversesummarize

Using n() with summarise_all


Works fine:

stats = c('mean', 'median', 'sd', 'max', 'min')
sumtable = iris %>% select(-Species) %>%  summarise_all(.funs = stats)

Doesn't work:

stats = c('mean', 'median', 'sd', 'max', 'min', 'n')
sumtable = iris %>% select(-Species) %>% summarise_all(.funs = stats)
Error in summarise_impl(.data, dots) : `n()` does not take arguments

Please advise.


Solution

  • I wanted this feature because I wanted to count non-missing observations. As Rohit pointed out, length would count all rows including missing obs. So what I did in the end was this:

    not.na = function(x) {sum(!is.na(x))}
    stats = c('mean', 'median', 'sd', 'max', 'min', 'not.na')
    sum.acs = acs %>% group_by(year) %>% summarise_all(.funs = stats)