Can you list an exception to tidyselect `everything()`

末鹿安然 提交于 2020-01-06 05:17:09

问题


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

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") %>% 
... %>% 

and I get the following error:

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


回答1:


You would do

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

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

iris %>% as_tibble() %>% select(setdiff(everything(),match("Species",names(.))))
#> # 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

(or just iris %>% as_tibble() %>% select(setdiff(everything(), 5)) if it's acceptable)



来源:https://stackoverflow.com/questions/58531982/can-you-list-an-exception-to-tidyselect-everything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!