用 dplyr "other then" 组进行总结
Summarize with dplyr "other then" groups
我需要在分组 data_frame 中进行总结(警告:非常感谢使用 dplyr 的解决方案,但不是强制性的)每个组(简单)和 [=30 上的相同内容=]组。
最小示例
if(!require(pacman)) install.packages(pacman)
pacman::p_load(dplyr)
df <- data_frame(
group = c('a', 'a', 'b', 'b', 'c', 'c'),
value = c(1, 2, 3, 4, 5, 6)
)
res <- df %>%
group_by(group) %>%
summarize(
median = median(value)
# median_other = ... ??? ... # I need the median of all "other"
# groups
# median_before = ... ??? ... # I need the median of groups (e.g
# the "before" in alphabetic order,
# but clearly every roule which is
# a "selection function" depending
# on the actual group is fine)
)
我的预期结果如下
group median median_other median_before
a 1.5 4.5 NA
b 3.5 3.5 1.5
c 5.5 2.5 2.5
我搜索了 Google 类似于 "dplyr summarize excluding groups"、"dplyr summarize other then group" 的字符串,我搜索了 dplyr 文档,但找不到解决方案。
这里,这个 () 不适用,因为它只在总和上运行,即是一个解决方案 "function-specific"(并且有一个简单的算术函数,没有考虑每个组的可变性).更复杂的函数请求(即均值、sd 或用户函数)呢? :-)
感谢大家
PS:summarize()
是一个例子,同样的问题导致 mutate()
或其他基于组工作的 dplyr 函数。
我认为一般不可能对summarise()
内的其他组进行操作(即我认为其他组不是"visible"总结某个组)。您可以定义自己的函数并在 mutate 中使用它们以将它们应用于某个变量。对于更新的示例,您可以使用
calc_med_other <- function(x) sapply(seq_along(x), function(i) median(x[-i]))
calc_med_before <- function(x) sapply(seq_along(x), function(i) ifelse(i == 1, NA, median(x[seq(i - 1)])))
df %>%
group_by(group) %>%
summarize(med = median(value)) %>%
mutate(
med_other = calc_med_other(med),
med_before = calc_med_before(med)
)
# group med med_other med_before
# (chr) (dbl) (dbl) (dbl)
#1 a 1.5 4.5 NA
#2 b 3.5 3.5 1.5
#3 c 5.5 2.5 2.5
这是我的解决方案:
res <- df %>%
group_by(group) %>%
summarise(med_group = median(value),
med_other = (median(df$value[df$group != group]))) %>%
mutate(med_before = lag(med_group))
> res
Source: local data frame [3 x 4]
group med_group med_other med_before
(chr) (dbl) (dbl) (dbl)
1 a 1.5 4.5 NA
2 b 3.5 3.5 1.5
3 c 5.5 2.5 3.5
我试图想出一个全 dplyr 解决方案,但基础 R 子集化工作得很好 median(df$value[df$group != group])
返回不在当前组中的所有观察值的中值。
希望本文能帮助您解决问题。
我需要在分组 data_frame 中进行总结(警告:非常感谢使用 dplyr 的解决方案,但不是强制性的)每个组(简单)和 [=30 上的相同内容=]组。
最小示例
if(!require(pacman)) install.packages(pacman)
pacman::p_load(dplyr)
df <- data_frame(
group = c('a', 'a', 'b', 'b', 'c', 'c'),
value = c(1, 2, 3, 4, 5, 6)
)
res <- df %>%
group_by(group) %>%
summarize(
median = median(value)
# median_other = ... ??? ... # I need the median of all "other"
# groups
# median_before = ... ??? ... # I need the median of groups (e.g
# the "before" in alphabetic order,
# but clearly every roule which is
# a "selection function" depending
# on the actual group is fine)
)
我的预期结果如下
group median median_other median_before
a 1.5 4.5 NA
b 3.5 3.5 1.5
c 5.5 2.5 2.5
我搜索了 Google 类似于 "dplyr summarize excluding groups"、"dplyr summarize other then group" 的字符串,我搜索了 dplyr 文档,但找不到解决方案。
这里,这个 (
感谢大家
PS:summarize()
是一个例子,同样的问题导致 mutate()
或其他基于组工作的 dplyr 函数。
我认为一般不可能对summarise()
内的其他组进行操作(即我认为其他组不是"visible"总结某个组)。您可以定义自己的函数并在 mutate 中使用它们以将它们应用于某个变量。对于更新的示例,您可以使用
calc_med_other <- function(x) sapply(seq_along(x), function(i) median(x[-i]))
calc_med_before <- function(x) sapply(seq_along(x), function(i) ifelse(i == 1, NA, median(x[seq(i - 1)])))
df %>%
group_by(group) %>%
summarize(med = median(value)) %>%
mutate(
med_other = calc_med_other(med),
med_before = calc_med_before(med)
)
# group med med_other med_before
# (chr) (dbl) (dbl) (dbl)
#1 a 1.5 4.5 NA
#2 b 3.5 3.5 1.5
#3 c 5.5 2.5 2.5
这是我的解决方案:
res <- df %>%
group_by(group) %>%
summarise(med_group = median(value),
med_other = (median(df$value[df$group != group]))) %>%
mutate(med_before = lag(med_group))
> res
Source: local data frame [3 x 4]
group med_group med_other med_before
(chr) (dbl) (dbl) (dbl)
1 a 1.5 4.5 NA
2 b 3.5 3.5 1.5
3 c 5.5 2.5 3.5
我试图想出一个全 dplyr 解决方案,但基础 R 子集化工作得很好 median(df$value[df$group != group])
返回不在当前组中的所有观察值的中值。
希望本文能帮助您解决问题。