R dplyr:丢弃多个列 [英] R dplyr: Drop multiple columns

查看:159
本文介绍了R dplyr:丢弃多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框和该数据框中列的列表,我想放弃。我们以 iris 数据集为例。我想删除 Sepal.Length Sepal.Width 并仅使用剩余的列。如何使用 dplyr 或 select _ >包?

I have a dataframe and list of columns in that dataframe that I'd like to drop. Let's use the iris dataset as an example. I'd like to drop Sepal.Length and Sepal.Width and use only the remaining columns. How do I do this using select or select_ from the dplyr package?

以下是我迄今为止所尝试的:

Here's what I've tried so far:

drop.cols <- c('Sepal.Length', 'Sepal.Width')
iris %>% select(-drop.cols)




-drop.cols中的错误:一元运算符的无效参数

Error in -drop.cols : invalid argument to unary operator



iris %>% select_(.dots = -drop.cols)




-drop.cols中的错误:一元运算符的无效参数

Error in -drop.cols : invalid argument to unary operator



iris %>% select(!drop.cols)




错误!drop.cols:无效的参数类型

Error in !drop.cols : invalid argument type



iris %>% select_(.dots = !drop.cols)




错误!drop.cols:无效的参数类型

Error in !drop.cols : invalid argument type

我觉得我是发出一些明显的东西,因为这似乎是一个应该已经存在的非常有用的操作。在Github上,有人发布了类似的问题,Hadley表示使用负数索引。这是我想过的(我想),但没有效果。任何建议?

I feel like I'm missing something obvious because these seems like a pretty useful operation that should already exist. On Github, someone posted a similar issue, and Hadley said to use 'negative indexing'. That's what (I think) I've tried, but to no avail. Any suggestions?

推荐答案

请查看select_vars上的帮助。在这种情况下:

Check the help on select_vars. That gives you some extra ideas on how to work with this.

In your case:

iris%>%select(-one_of(drop.cols))

这篇关于R dplyr:丢弃多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆