r - 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.
解决方案
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))
推荐阅读
- python - 跨多个 API 调用保留 SSH 会话
- python - 我怎样才能使这个完美平方和函数更有效?
- javascript - 如何在具有私有方法的 Jest 中模拟 http 方法
- android - Android Retrofit,如何阻止同一请求的多个副本
- angular - 如何在移动顶点时重新调整 mxgraph 边缘以使其看起来不奇怪?
- javascript - 表单提交的firebase更新不起作用?
- python - 在 SQLAlchemy 中使用设置器时获取 AttributeError
- python - 使用 conda 或 pip 在 windows 上安装 conda
- c# - 在 C# 中为 SQLite 形成数据结构时遇到问题
- alpha - 在巴比伦网格颜色顶点中使用 alpha