I have a large spreaded data frame:
df: a1 a2 a3 a4 a5 ...............
r w sd w y ........
I have another input which is a subset of df.
subset_df: a3 a4 a5
f e u
My goal is to take the column names of subset_df
, select these columns in df
and continue from there (in my case to compare the values).
When I do this the simple way:
df[,names(subset_df)]
it works, but why it refuses to work with dplyr select
?
Here is the error when running:
names_sub_df <- names(subset_df)
df %>% select(names_sub_df)
Error: All select() inputs must resolve to integer column positions.
The following do not:
* as.vector(names_sub_df)
Here is a reproducible example:
key <- c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17", "a18")
value <- c("G", "CTT", "C", "C", "G", "C", "T", "C", "C", "C", "G", "T", "C", "G", "T", "A", "T", "G")
test2 <- data.frame(key, value, stringsAsFactors = FALSE)
library(tidyr)