Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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]) 

where

> 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.

Thanks.

like image 564
KirkH Avatar asked Feb 26 '14 00:02

KirkH


People also ask

What does dplyr :: select do in R?

We can make a new data table by choosing or selecting just the variables that we are interested in. That is what the function select of the dplyr package does. We use the select function to tell R what variables or columns of our data set we want to keep.

What is use of select () function of dplyr package?

select() function in dplyr which is used to select the columns based on conditions like starts with, ends with, contains and matches certain criteria and also selecting column based on position, Regular expression, criteria like selecting column names without missing values has been depicted with an example for each.


1 Answers

In more recent versions of dplyr, this is possible in select with one_of, as in

my_cols <- c('mpg', 'disp') mtcars %>% select(one_of(my_cols)) 
like image 198
slizb Avatar answered Oct 05 '22 16:10

slizb