pomegranate
pomegranate

Reputation: 765

Pass column name as a parameter to piped statement

I've browsed around other questions and know that in most cases, I can just directly pass the column name as a string and then use d[,column] to access the values. However, I'm unable to get it to work in my function that uses piped statements (the function takes the given column and rounds values to the nearest 20):

fun = function(mydata, column) {
   new = mydata %>%
       mutate(time = plyr::round_any(column,                  #also tried mydata[,column]
                              20, f = floor))
   return(new)
}

Any suggestions on how to do this?

In reference to joran's answer below: how might I be able to add in column names passed as arguments between column names that remain constant? What function does quote have?

E.g.

fun2 = function(d, column, column2) {
    d %>%
    mutate_(time = interp(quote(plyr::round_any(var,20,f = floor)),
                                            var = as.name(column))) %>%
    count_(CONSTANT_COL_NAME, interp(var, var=as.name(column2)), CONSTANT_COL_NAME2) #Line in question
}

Upvotes: 1

Views: 69

Answers (1)

joran
joran

Reputation: 173677

I believe this works using interp from lazyeval:

library(dplyr)
library(lazyeval)

d <- data.frame(x = rnorm(10,300,20))
column <- "x"
> d %>%
    mutate_(time = interp(quote(plyr::round_any(var,20,f = floor)),
                                                var = as.name(column)))

          x time
1  299.8697  280
2  271.5919  260
3  298.2845  280
4  276.3756  260
5  312.3145  300
6  289.7780  280
7  305.9819  300
8  290.5856  280
9  306.8417  300
10 336.2645  320

Upvotes: 2

Related Questions