1

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
}
pomegranate
  • 755
  • 5
  • 19
  • take a look at this http://stackoverflow.com/questions/30108105/dplyr-standard-evaluation-for-mutate-with-quoted-variable-names – MLavoie Dec 28 '15 at 19:37

1 Answers1

2

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
joran
  • 169,992
  • 32
  • 429
  • 468
  • Thanks! This works great. Followup question (and to better understand how interp works -- how might I add another column to it, so that the function now takes two column names? (Adding to question above for formatting) – pomegranate Dec 28 '15 at 20:42
  • @pomegranate Your additional question doesn't make much sense w/respect to count, `column1 <- 'y2'; d %>% count_(vars = c('y1',column1))`. It works differently. If you read the non-standard evaluation vignette in the documentation you'll see that `quote` is only one of the options. I chose it because it seemed simplest in this case since you're calling another function. – joran Dec 28 '15 at 21:06