如何在group_by中传递变量名 [英] How to pass a variable name in group_by

查看:180
本文介绍了如何在group_by中传递变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码计算组name1中数据框df中值(val)的排名:

I can calculate the rank of the values (val) in my dataframe df within the group name1 with the code:

res  <- df %>% arrange(val) %>% group_by(name1) %>% mutate(RANK=row_number()) 

我不想在代码中写入 name1列,而是希望将其作为变量传递,例如crit = name1。但是,下面的代码不起作用,因为假定crit1是列名而不是变量名。

Instead of writing the column "name1" in the code, I want to pass it as variable, eg crit = "name1". However, the code below does not work since crit1 is assumed to be the column name instead of a variable name.

res  <- df %>% arrange(val) %>% group_by(crit1) %>% mutate(RANK=row_number()) 

如何在代码中传递crit1?

How can I pass crit1 in the code?

谢谢,
Tom

Thanks, Tom

推荐答案

我们可以使用 group_by _

library(dplyr)
df %>%
    arrange(val) %>% 
    group_by_(.dots=crit1) %>%
    mutate(RANK=row_number()) 
#Source: local data frame [10 x 4]
#Groups: name1, name2 [7]

#            val name1 name2  RANK
#          <dbl> <chr> <chr> <int>
#1  -0.848370044     b     c     1
#2  -0.583627199     a     a     1
#3  -0.545880758     a     a     2
#4  -0.466495124     b     b     1
#5   0.002311942     a     c     1
#6   0.266021979     c     a     1
#7   0.419623149     c     b     1
#8   0.444585270     a     c     2
#9   0.536585304     b     a     1
1#0  0.847460017     a     c     3



更新



group_by _ 在最新版本中已弃用(现在使用 dplyr 版本- 0.8.1 ),因此我们可以使用 group_by_at 将字符串向量作为输入变量

Update

group_by_ is deprecated in the recent versions (now using dplyr version - 0.8.1), so we can use group_by_at which takes a vector of strings as input variables

df %>%
  arrange(val) %>% 
  group_by_at(crit1) %>%
  mutate(RANK=row_number())






或者另一种选择是转换为符号( syms 来自 rlang )并评估( !!!

df %>%
   arrange(val) %>% 
   group_by(!!! rlang::syms(crit1)) %>% 
   mutate(RANK = row_number())



data



data

set.seed(24)
df <- data.frame(val = rnorm(10), name1= sample(letters[1:3], 10, replace=TRUE), 
         name2 = sample(letters[1:3], 10, replace=TRUE), 
 stringsAsFactors=FALSE)

crit1 <- c("name1", "name2")

这篇关于如何在group_by中传递变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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