Dplyr write a function with column names as inputs

后端 未结 2 1758
梦如初夏
梦如初夏 2020-12-21 16:21

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

2条回答
  •  春和景丽
    2020-12-21 17:03

    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 "".

提交回复
热议问题