I am writing a function that I will use in several columns in dplyr, but I am unable to pass the column names as inputs to the functions for dplyr.
Here is an example of what I want to do:
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("var1")
The result should look like
df %>%
group_by(group)%>%
summarize(output=mean(sqrt(var1)))%>%
select(output)
I found several similar questions, but nothing that I could directly apply to my problem, so any help is appreciated. I tried some solutions related to eval, but I honestly don’t know what exactly I should switch to eval.
source
share