将整个分组的数据框传递到一个函数总结
使用新的总结
dplyr 1.0.0可以传递一个函数以汇总比返回tibble,
library(tidyverse)
df <- tibble(
grp = rep(1:2, each = 5),
x = c(rnorm(5, -0.25, 1), rnorm(5, 0, 1.5)),
y = c(rnorm(5, 0.25, 1), rnorm(5, 0, 0.5)),
)
quibble <- function(x, q = c(0.25, 0.5, 0.75)) {
tibble(x = quantile(x, q), q = q)
}
df %>%
group_by(grp) %>%
summarise(quibble(x, c(0.25, 0.5, 0.75)))
我想知道是否可以将功能传递到incoper
总结整个分组的数据框架,而不是列的名称。 例如
df %>%
group_by(grp) %>%
summarise(my_function(d))
d
是第一个df
for grp == 1
,然后df
for grp ================================= 2
。
我认为我知道如何使用purrr :: Map
进行操作,
df %>%
group_by(grp) %>%
nest() %>%
mutate(temp = map(data, my_function)) %>%
unnest(temp)
但是我想知道是否可以使用总结
进行操作。
With the new summarise
from dplyr 1.0.0 it is possible to pass a function to summarise than returns a tibble
library(tidyverse)
df <- tibble(
grp = rep(1:2, each = 5),
x = c(rnorm(5, -0.25, 1), rnorm(5, 0, 1.5)),
y = c(rnorm(5, 0.25, 1), rnorm(5, 0, 0.5)),
)
quibble <- function(x, q = c(0.25, 0.5, 0.75)) {
tibble(x = quantile(x, q), q = q)
}
df %>%
group_by(grp) %>%
summarise(quibble(x, c(0.25, 0.5, 0.75)))
I wonder whether it is possible to pass to the function inside summarise
the whole grouped data frame instead of the names of the columns.
Something like
df %>%
group_by(grp) %>%
summarise(my_function(d))
where d
is first df
for grp == 1
and then df
for grp == 2
.
I think that I know how to do it with purrr::map
df %>%
group_by(grp) %>%
nest() %>%
mutate(temp = map(data, my_function)) %>%
unnest(temp)
but I wonder whether it is possible to do it with summarise
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)