KirkH KirkH - 1 year ago 58
R Question

how can i tell select() in dplyr that the string it is seeing is a column name in a data frame

I tried searching but didn't find an answer to this question.

I'm trying to use the select statement in dplyr but am having problems when I try to send it strings. My question is, how do i tell select() that the string that it is seeing is a column name in the data frame?

e.g. this works fine

select(df.main.scaled, var1, var3)
select(df.main.scaled, var2, var4)

but this does not work:

select(df.main.scaled, names.gens[i,1], names.gens[i,2])


> names.genx <- c("var1","var2")
> names.geny <- c("var3","var4")
> names.gens <- cbind(names.genx, names.geny)
> names.gens
names.genx names.geny
[1,] "var1" "var3"
[2,] "var2" "var4"

To be clear, all the strings in names.gens are column names in the data frame.


Answer Source

I figured it out through trial and error. If anybody is curious, did something like this:

select(df.main.scaled, eval(names.gens[[i]]), eval(names.gens[[i+someindex]]))