Home > Mobile >  R studio: how do I subset only columns that match a list?
R studio: how do I subset only columns that match a list?

Time:01-20

I feel like this isnt a hard question, but an hour of googling has gotten me no where.

say I have the following DF

Column A Column B Column C
Cell 1 Cell 2 Cell 2
Cell 3 Cell 4 Cell 4

I have hundreds of columns though, and I'm only interested in a specific list. Lets say this list includes "Column A" and "Column C"

I want to then filter the dataframe so it only contains these columns

Column A Column C
Cell 1 Cell 2
Cell 3 Cell 4

How do I do this?

I tried

which(colnames(data) %in% list)

match(list, names(data))

subset(data, names(data) %in% list)

CodePudding user response:

Just use this

subset(data, select = names(data) %in% list)

The argument select used to indicating columns to select from a data frame.

CodePudding user response:

You can install "tidyverse" package and use the command (dataname here) %>% select_if(condition here) which gives columns only if the column meets the condition. Here is a article by DataNovia that gives examples as well as other functions similar to it. Hope this helps :)

  • Related