R:如何在分组数据中使用汇总和功能(R: How to use summarize and do function together on grouped data)

编程入门 行业动态 更新时间:2024-10-11 23:22:22
R:如何在分组数据中使用汇总和功能(R: How to use summarize and do function together on grouped data)

在tidyverse中,汇总可用于具有单值函数的分组数据。 例如

mtcars %>% group_by(cyl) %>% summarise(max(cos(mpg)))

如果函数是矢量值,那么,如果我没有错,建议使用do。 例如,do命令适用于phych包中向量值函数'describe':

library(psych) mtcars %>% group_by(cyl) %>% do(describe(.$mpg))

如何同时将单值和向量值函数应用于分组数据? 例如,如何将max(cos())和describe()同时应用于mpg列,并将输出作为一个数据帧?

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?

最满意答案

我们可以将describe的输出放在summarise list中,然后再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

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

更多推荐

本文发布于:2023-07-19 07:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1175440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   数据   如何在   summarize   grouped

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!