确实很难将dplyr函数放到我的函数中。我理解function_
标准评估版的后缀,但是仍然有问题,并且似乎尝试了eval
paste
和的所有组合lazy
。
试图用一组控件的中位数除以多列。示例数据包括虹膜中名为“ Control”的附加列,因此每个物种有40个“ normal”和10个“ control”。
data(iris)
control <- rep(c(rep("normal", 40), rep("control", 10)), 3)
iris$Control <- control
普通dplyr可以正常工作:
out_df <- iris %>%
group_by(Species) %>%
mutate_each(funs(./median(.[Control == "control"])), 1:4)
尝试将其包装成一个函数:
norm_iris <- function(df, control_col, control_val, species, num_cols = 1:4){
out <- df %>%
group_by_(species) %>%
mutate_each_(funs(./median(.[control_col == control])), num_cols)
return(out)
}
norm_iris(iris, control_col = "Control", control_val = "control", species = "Species")
我得到了错误:
Error in UseMethod("as.lazy_dots") :
no applicable method for 'as.lazy_dots' applied to an object of class "c('integer', 'numeric')"
使用funs_
而不是funs
我得到Error:...: need numeric data
如果您还没有这样做,它可能会帮助您在此处阅读有关标准评估的小插图,尽管听起来其中有些可能很快就会改变。
你的功能缺少使用interp
从包lazyeval的mutate_each_
线。由于您尝试在中使用变量名(Control
变量),因此在这种情况下funs
,您需要funs_
和一起使用interp
。请注意,这是您根本不需要的mutate_each_
情况。如果在选择要突变的列时尝试使用列名而不是列号,则将需要它。
这是该行在您的函数中的样子,而不是您所拥有的:
mutate_each(funs_(interp(~./median(.[x == control_val]), x = as.name(control_col))),
num_cols)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句