view raw
user2533660 user2533660 - 9 months ago 97
R Question

R - How do I pass the value of a variable as an argument?

Noob here. I am trying to pass the value of "xmove1," "xmove2," and "xmove3" into the filter command.

xmove1 <- "X3"
xmove2 <- "X1"
xmove3 <- "X5"

xtemp <- filter(fullTable, xmove1 == 1, xmove2 == 2, xmove3 == 3)

The problem is that instead of reading the value of xmove1, it seems to be reading it as a string - "xmove1."

I have also tried to use "," but the result seems to be the same.

xtemp <- filter(fullTable, == 1, == 2, == 3)


If you want to dynamically supply column names, use the standard evaluation alternatives of the dplyr function (see vignette("nse") for more info).

You can

filter_(fullTable, .dots=list(
    bquote(.( == 1), 
    bquote(.( == 2), 
    bquote(.( == 3)))