Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select_at() drop some vars, pull some to front and then everything() in one call?

Tags:

r

dplyr

Example, I want to drop field mpg, select carb so that it's first, then just everything that's left over in their existing order.

mtcars |> select_at(vars(-mpg, carb, everything()))

This seems to drop mpg as desired, but carb is not in the front position / first variable.

My call to select_at() was intended to read in english 'drop mpg, then select carb first then everything else'.

On the docs for ?vars it says to use across. I'm open to either, but would prefer a one liner if possible as opposed to e.g. select(-mpg) |> select_at(vars(carb, everything()))

like image 623
Doug Fir Avatar asked Sep 06 '25 02:09

Doug Fir


1 Answers

The order can be changed - i.e. place the column that needs to be deleted as the last entry

library(dplyr)
mtcars |> 
 select_at(vars(carb, everything(), -mpg)) |> 
 head()
                  carb cyl disp  hp drat    wt  qsec vs am gear
Mazda RX4            4   6  160 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag        4   6  160 110 3.90 2.875 17.02  0  1    4
Datsun 710           1   4  108  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive       1   6  258 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout    2   8  360 175 3.15 3.440 17.02  0  0    3
Valiant              1   6  225 105 2.76 3.460 20.22  1  0    3

The _at/_all etc are all deprecated. We can directly use everything() within select

 mtcars |> 
   select(carb, everything(), -mpg) |> 
   head()
                  carb cyl disp  hp drat    wt  qsec vs am gear
Mazda RX4            4   6  160 110 3.90 2.620 16.46  0  1    4
Mazda RX4 Wag        4   6  160 110 3.90 2.875 17.02  0  1    4
Datsun 710           1   4  108  93 3.85 2.320 18.61  1  1    4
Hornet 4 Drive       1   6  258 110 3.08 3.215 19.44  1  0    3
Hornet Sportabout    2   8  360 175 3.15 3.440 17.02  0  0    3
Valiant              1   6  225 105 2.76 3.460 20.22  1  0    3

The issue is that when we use -mpg as the first entry, it removes that column keeping all the rest of the column, then adding 'carb' as second entry does nothing because 'carb' is already a column in the selected data and duplicates for column names are not allowed, the last everything() adds back the 'mpg' again.

> mtcars |> select_at(vars(-mpg)) |> head()
                  cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant             6  225 105 2.76 3.460 20.22  1  0    3    1
> mtcars |> select_at(vars(-mpg, carb)) |> head()
                  cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant             6  225 105 2.76 3.460 20.22  1  0    3    1
> mtcars |> select_at(vars(-mpg, carb, everything())) |> head()
                  cyl disp  hp drat    wt  qsec vs am gear carb  mpg
Mazda RX4           6  160 110 3.90 2.620 16.46  0  1    4    4 21.0
Mazda RX4 Wag       6  160 110 3.90 2.875 17.02  0  1    4    4 21.0
Datsun 710          4  108  93 3.85 2.320 18.61  1  1    4    1 22.8
Hornet 4 Drive      6  258 110 3.08 3.215 19.44  1  0    3    1 21.4
Hornet Sportabout   8  360 175 3.15 3.440 17.02  0  0    3    2 18.7
Valiant             6  225 105 2.76 3.460 20.22  1  0    3    1 18.1

According to ?select, the usage is

select(.data, ...)

where ... is variadic argument, which can take any number of column names, numbers etc.

The order of evaluation happens from left to right, thus first expression is evaluated, then second and so on ...

like image 197
akrun Avatar answered Sep 08 '25 16:09

akrun