简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM