我需要根据我总结的变量,使用不同的汇总函数对数据帧进行分组和汇总。这些函数可以有不同的主参数和可选参数,我想编写一个可以完成所有这些操作的函数。
以下是我设法编写的更简单的函数,只是为了显示它的逻辑。
require(tidyverse)
require(magrittr)
require(rlang)
example <- data.frame(y = as.factor(c('A','B','C','A','B')),
x1 = c(7, 10, NA, NA, 2),
x2 = c(13, 0, 0, 2, 1),
z = c(0, 1, 0, 1, 0))
# Summarise variables with common prefix
do_summary_prefix <- function(dataset, y, prefix, fun, ...){
y <- enquo(y)
prefix <- quo_name(enquo(prefix))
fun <- match.fun(fun)
dataset %<>%
group_by(!!y) %>%
summarise_at(vars(starts_with(prefix)), funs(fun), ...) %>%
ungroup()
return(dataset)
}
do_summary_prefix(example, y, x, 'quantile', probs = 0.25, na.rm = T)
# Summarise variables with different names, one at a time
do_summary_x <- function(dataset, y, x, fun, ...){
y <- enquo(y)
x <- enquo(x)
dataset %<>%
group_by(!!y) %>%
summarise(!!paste(quo_name(x), fun, sep = '_') := do.call(match.fun(fun), list(x = !!x, ...))) %>%
ungroup()
return(dataset)
}
do_summary_x(example, y, x1, fun = 'mean', na.rm = F)
这对我来说没问题,我可以在我想要总结的变量的循环中使用do_summary_x
来完成工作。但我想将循环集成到更高级别的函数中,利用...
,同时仍然能够为我的汇总函数使用不同的参数。
我知道我不能将...
用于不同的子级函数,因此我将传递前一个(我的变量或函数参数)作为列表,并使用do.call
。对于输入变量保留...
并使用列表添加始终命名的参数更为自然。这就是我所说的:
#install.packages('plyr') # if needed
join_all <- plyr::join_all
do_summary <- function(dataset, y, ..., fun, other_args = list(NULL =
NULL)){
y_quo <- enquo(y)
y_name <- quo_name(y_quo)
values <- quos(...)
datasets <- lapply(values, function(value){
summarised_data <- dataset %>%
group_by(!!y_quo) %>%
summarise(calcul = do.call(fun,
unlist(list(list(x = !!value),
other_args),
recursive = F))) %>%
ungroup() %>%
rename(!!paste(quo_name(value), stat, sep = '_') := calcul)
return(summarised_data)
})
finished <- join_all(datasets, by = y_name, type = 'left')
return(finished)
}
do_summary(example, y,
x1, x2, z,
stat = 'quantile',
other_args = list(probs = 0.1, na.rm = T))
do_summary(example, y,
x1, x2, z,
fun = 'mean')
这个工作正常,所以我对它总体感到满意,但这只适用于具有x
第一个参数的函数。
假设我希望能够在此处更改fun
的第一个参数的名称,即x
。我该怎么做?
我还没有找到引用的解决方案然后注入do.call
之类的changing_arg = !!x
,或明智地使用list(!!changing_arg := !!x)
答案 0 :(得分:1)
以下是我如何简化您的功能:
library(dplyr)
library(rlang)
do_summary <- function(dataset, y, ..., fun, other_args = list(NULL = NULL)){
y_quo <- enquo(y)
values <- quos(...)
datasets <- dataset %>%
group_by(!!y_quo) %>%
summarise_at(vars(!!!values), .funs = fun, !!!other_args) %>%
rename_at(vars(!!!values), paste, fun, sep = "_")
return(datasets)
}
do_summary(example, y,
x1, x2, z,
fun = 'quantile',
other_args = list(probs = 0.1, na.rm = T))
do_summary(example, y,
x1, x2, z,
fun = 'mean')
<强>结果:强>
# A tibble: 3 x 4
y x1_quantile x2_quantile z_quantile
<fctr> <dbl> <dbl> <dbl>
1 A 7.0 3.1 0.1
2 B 2.8 0.1 0.1
3 C NA 0.0 0.0
# A tibble: 3 x 4
y x1_mean x2_mean z_mean
<fctr> <dbl> <dbl> <dbl>
1 A NA 7.5 0.5
2 B 6 0.5 0.5
3 C NA 0.0 0.0
备注:强>
您可以只使用lapply
和values
并将summarise_at
提供给{{1},而不是rename_at
遍历每个values
。使用vars
进行显式拼接。
!!!
提供给fun
的{{1}}参数,再次,您可以使用.funs
明确拼接summarise_at
。例如,other_args
变为!!!
。
list(probs = 0.1, na.rm = T)
同样的想法。使用probs = 0.1, na.rm = T
并明确拼接rename_at
。另一种方法是编写vars
,因为values
仅返回分组列和摘要列。
此方法摆脱了rename_at(vars(-y_name), ...)
,summarise_at
中的lapply
和最后的do.call
summarise
因此也不需要)。
join_all
末尾的y_name
来电似乎是一个错字,而不是do_summary
,我认为你的意思是quantile
请注意,此功能仅在您以字符串形式提供功能名称时才有效。