Dplyr write a function with column names as inputs

后端 未结 2 1759
梦如初夏
梦如初夏 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:00

    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
    

提交回复
热议问题