您能列出tidyselectʻeverything()`的例外吗 [英] Can you list an exception to tidyselect `everything()`

查看:106
本文介绍了您能列出tidyselectʻeverything()`的例外吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(tidyverse)
iris %>% as_tibble() %>% select(everything())

#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # ... with 140 more rows

说我想选择 iris 数据框,但种类除外。我如何在使用 tidyselect :: everything()时列出此例外?

Say I want to select everything in the iris data frame except Species. How do I list this one exception while utilizing tidyselect::everything()?

我的实际管道如下,并且

My actual pipe is below, and when

... %>% 
group_by(`ID`) %>% 
fill(everything, .direction = "updown") %>% 
... %>% 

,然后出现以下错误:


错误:列 ID 无法修改,因为它是分组变量

Error: Column ID can't be modified because it's a grouping variable


推荐答案

您会这样做

iris %>% as_tibble() %>% select(-Species)

但假设您有充分的理由不希望这样做,可以使用 everything()

but assuming you have good reason not to want that, here's a way using everything()

iris %>% as_tibble() %>% select(setdiff(everything(), one_of("Species")))
#> # A tibble: 150 x 4
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <dbl>       <dbl>        <dbl>       <dbl>
#>  1          5.1         3.5          1.4         0.2
#>  2          4.9         3            1.4         0.2
#>  3          4.7         3.2          1.3         0.2
#>  4          4.6         3.1          1.5         0.2
#>  5          5           3.6          1.4         0.2
#>  6          5.4         3.9          1.7         0.4
#>  7          4.6         3.4          1.4         0.3
#>  8          5           3.4          1.5         0.2
#>  9          4.4         2.9          1.4         0.2
#> 10          4.9         3.1          1.5         0.1
#> # ... with 140 more rows

(或仅 iris%> %as_tibble()%>%select(setdiff(everything(),5))(如果可以)

这篇关于您能列出tidyselectʻeverything()`的例外吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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