我想用循环重写下面的代码来迭代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))
答案 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)) )