Dplyr writes a function with column names as input

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.

+1
source share
2 answers

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
+3
source

The accepted answer no longer works in R 3.6 / dplyr 0.8.

, !!as.name()

:

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) )

- , mutate, :=. , colname .

example_mutate<-function(colname){
  df %>%
    mutate(!!colname := sqrt(!!as.name(colname)))
}
example_mutate( quote(var1) )

quote() , "".

0

Source: https://habr.com/ru/post/1691621/


All Articles