user1923975 user1923975 - 1 year ago 125
R Question

Not fully understanding how SE works across the dplyr verbs

I'm trying to understand how SE works in

so I can use variables as inputs to these functions. I'm having some trouble with understanding how this works across the different functions and when I should be doing what. It would be really good to understand the logic behind this.

Here are some examples:


a <- c("x", "y", "z")
b <- c(1,2,3)
c <- c(7,8,9)

df <- data.frame(a, b, c)

The following is exactly why i'd use SE and the
variant of a function. I want to change the name of what's being mutated based on another variable.

#Normal mutate - copies b into a column called new
mutate(df, new = b)

#Mutate using a variable column names. Use mutate_ and the unqouted variable name. Doesn't use the name "new", but use the string "" <- "new"
mutate_(df, = "b")

#Do I need to use interp? Doesn't work
expr <- interp(~(val = b), val =
mutate_(df, expr)

Now I want to
in the same way. Not sure why my first attempt didn't work.

#Apply the same logic to filter_. the following doesn't return a result <- "z"
filter_(df, "a" ==

#Do I need to use interp? Works. What's the difference compared to the above?
expr <- interp(~(a == val), val =
filter_(df, expr)

Now I try to
. Works as expected

#Apply the same logic to select_, an unqouted variable name works fine <- "b"

Now I move on to
. Knowing what worked for
and knowing that I had to use
, I try the following

#Now let's try to rename. Qouted constant, unqouted variable. Doesn't work <- "NEW"
rename_(df, "a" =

#Do I need an eval here? It worked for the filter so it's worth a try. Doesn't work 'Error: All arguments to rename must be named.'
expr <- interp(~(a == val), val =
rename_(df, expr)

Any tips on best practice when it comes to using variable names across the
functions and when
is required would be great.

Answer Source

The differences here are not related to which dplyr verb you are using. They are related to where you are trying to use the variable. You are mixing whether the variable is used as a function argument or not, and whether it should be interpreted as a name or as a character string.

Scenario 1:

You want to use your variable as an argument name. Such as in your mutate example.

mutate(df, new = b)

Here new is the name of a function argument, it is left of a =. The only way to do this is to use the .dots argument. Like <- 'new'
mutate_(df, .dots = setNames(list(~b),

Running just setNames(list(~b), shows you how we have an expression (~b), which is going right of the =, and the name is going left of the =.

Scenario 2:

You want to give only a variable as a function argument. This is the simplest case. Let's again use mutate(df, new = b), but in this case we want b to be variable. We could use:

v <- 'b'
mutate_(df, .dots = setNames(list(v), 'new'))

Or simply:

mutate_(df, new = b)

Scenario 3

You want to do some combinations of variable and fixed things. That is, your expression should only be partly variable. For this we use interp. For example, what if we would like to do something like:

mutate(df, new = b + 1)

But being able to change b?

v <- 'b'    
mutate_(df, new = interp(~var + 1, var =

Note that we to make sure that we insert b into the expression, not 'b'.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download