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.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With