In tidyverse, summarise can be used on grouped data with single valued functions. For example
mtcars %>% group_by(cyl) %>% summarise(max(cos(mpg)))
If the function is vector-valued then, if I am not wrong, it's recommended to use do. For example, the do command works for the vector valued function 'describe' from phych package:
library(psych)
mtcars %>% group_by(cyl) %>% do(describe(.$mpg))
How to apply both a single-valued and a vector-valued functions to grouped data at the same time? For example, how to apply both max(cos()) and describe() to mpg column, and have the output as one dataframe?
We can place the output of describe
in a list
within the summarise
and then unnest
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
summarise(Cosmpg = max(cos(mpg)), list(describe(mpg))) %>%
unnest
# A tibble: 3 x 15
# cyl Cosmpg vars n mean sd median trimmed mad min max range skew kurtosis se
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4.00 0.743 1.00 11.0 26.7 4.51 26.0 26.4 6.52 21.4 33.9 12.5 0.259 -1.65 1.36
#2 6.00 0.939 1.00 7.00 19.7 1.45 19.7 19.7 1.93 17.8 21.4 3.60 -0.158 -1.91 0.549
#3 8.00 0.989 1.00 14.0 15.1 2.56 15.2 15.2 1.56 10.4 19.2 8.80 -0.363 -0.566 0.684
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.