如何使用不同的变量以编程方式汇总tbl?

时间:2015-11-08 11:34:59

标签: r dplyr

我想用循环重写下面的代码来迭代by-variables' a'和' b'。我怎么能这样做?

set.seed(1234)
mydata <- as.tbl(data.frame(a=1:12, b=runif(12), x=rnorm(12)))
mydata

library(dplyr)
library(Hmisc)

mydata %>%
  mutate(feature=cut2(a, g=3)) %>% 
  group_by(feature) %>%  # group by quantiles of 'a'
  summarise(x=mean(x))

mydata %>%
  mutate(feature=cut2(b, g=3)) %>% 
  group_by(feature) %>%  # group by quantiles of 'b'
  summarise(x=mean(x))

1 个答案:

答案 0 :(得分:1)

我们可以使用interp中的lazyeval

library(lazyeval)
lapply(c('a', 'b'), function(var)
                mydata %>% 
                  mutate_(feature=interp(~cut2(v1, g=3), v1=as.name(var))) %>% 
                          group_by(feature) %>%
                  summarise(x=mean(x)) )