首页 > 解决方案 > How to remove multiple columns from a dataframe with column list

问题描述

I have a variable list with column names and a dataframe . I would like to remove columns from the dataframes when the column names match the variable list.

columns -> "a","c"
dataframe->

a b c d  
0 0 1 1  
1 1 1 1  

Ouput->

b d  
0 1  
1 1

Please help me out with the solution.

标签: rdplyrtidyverse

解决方案


Update: Anders Swanson pointed out that you can now use select with standard evaluation. So the following works:

select(dataframe, -columns)

Previous version

You can use select_ together with '-' as shown below:

# create data 
columns <- c("a","c")
dataframe <- read.table(text="a b c d
0 0 1 1
1 1 1 1 ", header = TRUE)
# load dplyr package
require(dplyr)
# select columns
select_(dataframe, .dots = paste0("-", columns))

推荐阅读