R dplyr:删除多个列

Nav*_*nam 72 r dplyr

我有一个数据框和该数据框中的列列表,我想放弃.我们以iris数据集为例.我想放弃Sepal.LengthSepal.Width仅使用其余列.如何使用selectselect_dplyr包中执行此操作?

这是我到目前为止所尝试的:

drop.cols <- c('Sepal.Length', 'Sepal.Width')
iris %>% select(-drop.cols)
Run Code Online (Sandbox Code Playgroud)

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

iris %>% select_(.dots = -drop.cols)
Run Code Online (Sandbox Code Playgroud)

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

iris %>% select(!drop.cols)
Run Code Online (Sandbox Code Playgroud)

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

iris %>% select_(.dots = !drop.cols)
Run Code Online (Sandbox Code Playgroud)

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

我觉得我错过了一些明显的东西,因为这些似乎是一个非常有用的操作,应该已经存在.在Github上,有人发布了类似的问题,Hadley说要使用"负索引".这就是(我认为)我尝试过的,但无济于事.有什么建议?

phi*_*ver 110

查看select_vars上的帮助.这为您提供了一些关于如何使用它的额外想法.

在你的情况下:

iris %>% select(-one_of(drop.cols))
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.在文档方面,您如何首先了解这些功能? (8认同)
  • 我们应该在哪里找到像`one_of`这样的内置函数?除非我遗漏了某些东西,否则它不会出现在包文档中(`help(package ='dplyr')`). (4认同)
  • @geotheory,实际上one_of被记录在案.参见`help(one_of,package ="dplyr")`.至少在包版本0.5.0中.但阅读Hadley在其中一个软件包发布更新时发布的[博客](https://blog.rstudio.org)会有所帮助.一些功能记录在其他功能中.不幸的是,这需要阅读所有文档,当我想要一些不是很明显或可能与函数一起使用的东西时,我通常会这样做. (3认同)

Mig*_*lez 53

也试试

## Notice the lack of quotes
iris %>% select (-c(Sepal.Length, Sepal.Width))
Run Code Online (Sandbox Code Playgroud)

  • 大!当我们必须通过从控制台复制粘贴名称来删除列时非常有用. (4认同)

sbh*_*bha 24

除此之外,select(-one_of(drop.cols))还有一些其他选项可用于删除列select(),而不涉及定义所有特定列名称(使用dplyr starwars样本数据可以获得更多列名称):

starwars %>% 
  select(-(name:mass)) %>%        # the range of columns from 'name' to 'mass'
  select(-contains('color')) %>%  # any column name that contains 'color'
  select(-starts_with('bi')) %>%  # any column name that starts with 'bi'
  select(-ends_with('er')) %>%    # any column name that ends with 'er'
  select(-matches('^f.+s$')) %>%  # any column name matching the regex pattern
  select_if(~!is.list(.)) %>%     # not by column name but by data type
  head(2)

# A tibble: 2 x 2
homeworld species
  <chr>     <chr>  
1 Tatooine  Human  
2 Tatooine  Droid 
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,`~` 是定义匿名函数的 purrr 简写,它不是另一个符号。例如,这两个表示相同的东西 `function(x) {!is.list(x)}` 和 `~!is.list(.)`。将 `~` 视为 `function(.)` 的简写。 (3认同)

小智 6

注意这个select()函数,因为它在dplyr和MASS包中都使用过,所以如果加载了MASS,select()可能无法正常工作.要找出加载了哪些包sessionInfo(),请在"其他附加包:"部分中键入并查找它.如果已加载,请键入detach( "package:MASS", unload = TRUE ),您的select()功能应该再次起作用.

  • 或者您可以直接在包命名空间中访问该函数,因为````dplyr :: select()```. (10认同)
  • 我经常遇到这个问题。现在我通常在脚本顶部定义一个新函数“dselect &lt;- dplyr::select()”。 (2认同)

use*_*745 6

对于任何到达这里想要删除一系列的人。

最小可重复示例

删除一系列,如下所示:

iris %>% 
  select(-(Sepal.Width:Petal.Width)) %>% 
  head

#   Sepal.Length Species
# 1          5.1  setosa
# 2          4.9  setosa
# 3          4.7  setosa
# 4          4.6  setosa
# 5          5.0  setosa
# 6          5.4  setosa
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 列名周围的 , 很重要,必须(使用)


akr*_*run 5

我们可以尝试

iris %>% 
      select_(.dots= setdiff(names(.),drop.cols))
Run Code Online (Sandbox Code Playgroud)