mutate_each_非标准评估

斯科特·沃沙尔(Scott Warchal)

确实很难将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从包lazyevalmutate_each_线。由于您尝试在中使用变量名(Control变量),因此在这种情况下funs,您需要funs_和一起使用interp请注意,这是您根本不需要的mutate_each_情况。如果在选择要突变的列时尝试使用列名而不是列号,则将需要它。

这是该行在您的函数中的样子,而不是您所拥有的:

mutate_each(funs_(interp(~./median(.[x == control_val]), x = as.name(control_col))), 
                        num_cols)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章