dplyr::group_by_ 带几个变量名的字符串输入 [英] dplyr::group_by_ with character string input of several variable names

查看:16
本文介绍了dplyr::group_by_ 带几个变量名的字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,要求用户在函数调用中定义一个或多个分组变量.然后使用 dplyr 对数据进行分组,如果只有一个分组变量,它会按预期工作,但我还没有想出如何使用多个分组变量来实现.

I'm writing a function where the user is asked to define one or more grouping variables in the function call. The data is then grouped using dplyr and it works as expected if there is only one grouping variable, but I haven't figured out how to do it with multiple grouping variables.

示例:

x <- c("cyl")
y <- c("cyl", "gear")
dots <- list(~cyl, ~gear)

library(dplyr)
library(lazyeval) 

mtcars %>% group_by_(x)             # groups by cyl
mtcars %>% group_by_(y)             # groups only by cyl (not gear)
mtcars %>% group_by_(.dots = dots)  # groups by cyl and gear, this is what I want.

我尝试使用以下方法将 y 变成与 dots 相同的内容:

I tried to turn y into the same as dots using:

mtcars %>% group_by_(.dots = interp(~var, var = list(y)))
#Error: is.call(expr) || is.name(expr) || is.atomic(expr) is not TRUE

如何使用 > 1 个变量名的用户定义输入字符串(如示例中的 y)使用 dplyr 对数据进行分组?

How to use a user-defined input string of > 1 variable names (like y in the example) to group the data using dplyr?

(这个问题与 这个但没有在那里回答.)

(This question is somehow related to this one but not answered there.)

推荐答案

这里不需要interp,只需使用as.formula将字符串转换为公式即可:

No need for interp here, just use as.formula to convert the strings to formulas:

dots = sapply(y, . %>% {as.formula(paste0('~', .))})
mtcars %>% group_by_(.dots = dots)

您的 interp 方法不起作用的原因是该表达式返回以下内容:

The reason why your interp approach doesn’t work is that the expression gives you back the following:

~list(c("cyl", "gear"))

——不是你想要的.当然,您可以在 y 上使用 sapply interp ,这类似于使用上面的 as.formula:

– not what you want. You could, of course, sapply interp over y, which would be similar to using as.formula above:

dots1 = sapply(y, . %>% {interp(~var, var = .)})

不过,其实你也可以直接传y:

mtcars %>% group_by_(.dots = y)

关于非标准评估的 dplyr 小插图更详细地解释了这些方法之间的区别.

The dplyr vignette on non-standard evaluation goes into more detail and explains the difference between these approaches.

这篇关于dplyr::group_by_ 带几个变量名的字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆