0

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)

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • `data[list]` or `data[unlist(list)]` should do it, depending on if your "list" is actually a `vector` or a `list`. If that works without further complication, this is probably a duplicate question of something like: https://stackoverflow.com/questions/4605206/drop-data-frame-columns-by-name or https://stackoverflow.com/questions/45846341/how-to-subset-a-data-frame-column-wise-using-column-names – thelatemail Jan 19 '23 at 23:09

1 Answers1

1

Just use this

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

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

Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19
  • oh my god. yup, that worked. turned out the column names of my list all ended in an a or a z, while the column names of the dataframe didn't :) :) :) it's always something so stupid. Ty!!! – VintageLego Jan 19 '23 at 23:28