Display name
Display name

Reputation: 4481

Can you list an exception to 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

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

Upvotes: 7

Views: 1384

Answers (1)

moodymudskipper
moodymudskipper

Reputation: 47320

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(), 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

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

Upvotes: 6

Related Questions