Joel B Joel B - 3 months ago 10
R Question

remove variables from a data frame based on a column within another data frame in R

I want to remove a number of columns from df1

| A | B | C | D
| ------ | ------ | ------ | ------
1 | 0.870 | 0.435 | 0.968 | 0.679
2 | 0.456 | 0.259 | 0.906 | 0.467
3 | 0.298 | 0.256 | 0.457 | 0.768
4 | 0.994 | 0.987 | 0.365 | 0.765


if they appear as values within a column called TEST within df2

| TEST |
| ------ |
1 | A |
2 | B |

Answer
df1[,!(colnames(df1) %in% df2$TEST)]
Comments