6
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

Display name
  • 4,153
  • 5
  • 27
  • 75
  • 5
    Is there a reason why you wouldn't just do `iris %>% as_tibble() %>% select(-Species)`? – Mako212 Oct 23 '19 at 23:07
  • 4
    Also, you might need to `ungroup()` before you run your `select` command, if you're trying to remove a grouping variable. – Mako212 Oct 23 '19 at 23:11
  • @Mako212 I had to recreate the question to show my intent. Thanks for these clues. Other question here -> https://stackoverflow.com/questions/58542413/unable-to-use-tidyselect-everything-in-combination-with-group-by-and-fi – Display name Oct 24 '19 at 16:10

1 Answers1

5

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)

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • 1
    would `pivot_longer(cols = setdiff(everything(), one_of("Species")), names_to = "metric")` be a case worthy of using `setdiff`? in either case thanks for the suggestion, it works like a charm :) – Matteo Mar 01 '23 at 16:40
  • Doesn't `%>% pivot_longer(cols = -Species, names_to = "metric")` work ? – moodymudskipper Mar 01 '23 at 17:18