I\'m writing a function that I\'m going to use on multiple columns in dplyr, but I\'m having trouble passing column names as inputs to functions for dplyr.
Here\'s a
Is this what you expected?
df<-tbl_df(data.frame(group=rep(c("A", "B"), each=3), var1=sample(1:100, 6), var2=sample(1:100, 6)))
example<-function(colname){
df %>%
group_by(group)%>%
summarize(output=mean(sqrt(colname)))%>%
select(output)
}
example( quote(var1) )
#-----
Source: local data frame [2 x 1]
output
1 7.185935
2 8.090866
The accepted answer does not work anymore in R 3.6 / dplyr 0.8.
As suggested in another answer, one can use !!as.name()
This works for me:
df<-tbl_df(data.frame(group=rep(c("A", "B"), each=3), var1=sample(1:100, 6), var2=sample(1:100, 6)))
example<-function(colname){
df %>%
group_by(group)%>%
summarize(output=mean(sqrt(!!as.name(colname)))%>%
select(output)
}
example( quote(var1) )
If one additionally wants to have the column names to assign to in a mutate
, then the easiest is to use the assignment :=
. For example to replace colname with its square root.
example_mutate<-function(colname){
df %>%
mutate(!!colname := sqrt(!!as.name(colname)))
}
example_mutate( quote(var1) )
quote()
can of course be replaced with quotation marks ""
.