I have a data frame ("data") with lots and lots of columns. Some of the columns contain a certain string ("search_string").
How can I use dplyr::select()
to give me a subset including only the columns that contain the string?
I tried:
# columns as boolean vector
select(data, grepl("search_string",colnames(data)))
# columns as vector of column names names
select(data, colnames(data)[grepl("search_string",colnames(data))])
Neither of them work.
I know that select()
accepts numeric vectors as substitute for columns e.g.:
select(data,5,7,9:20)
But I don't know how to get a numeric vector of columns ID
s from my grepl()
expression.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…