How to use dplyr relocate in R

06.10.2021

Intro

The relocate method allows you to reorder the columns in a data set. The method is similar to select, but has some helpful methods for moving columns around. In this article, we will learn how to use the dplyr relocate method.

If you are in a hurry

If you don’t have time to read, here is a quick code snippet for you.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --

## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.0     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1

## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
mtcars %>% relocate(cyl, .before = drat)
##                      mpg  disp  hp cyl drat    wt  qsec vs am gear carb
## Mazda RX4           21.0 160.0 110   6 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0 160.0 110   6 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8 108.0  93   4 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4 258.0 110   6 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7 360.0 175   8 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1 225.0 105   6 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3 360.0 245   8 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4 146.7  62   4 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8 140.8  95   4 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2 167.6 123   6 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8 167.6 123   6 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4 275.8 180   8 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3 275.8 180   8 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2 275.8 180   8 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4 472.0 205   8 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4 460.0 215   8 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7 440.0 230   8 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4  78.7  66   4 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4  75.7  52   4 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9  71.1  65   4 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5 120.1  97   4 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5 318.0 150   8 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2 304.0 150   8 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3 350.0 245   8 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2 400.0 175   8 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3  79.0  66   4 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0 120.3  91   4 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4  95.1 113   4 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8 351.0 264   8 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7 145.0 175   6 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0 301.0 335   8 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4 121.0 109   4 4.11 2.780 18.60  1  1    4    2

Loading the Library

We can load the dplyr package directly, but I recommend loading the tidyverse package as we will use some other features in side.

library(tidyverse)

Loading the Dataset

For this tutorial, we will use the mtcars data set the comes with tidyverse. We take a look at this data set below.

data(mtcars)

glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,~
## $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,~
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16~
## $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180~
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,~
## $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.~
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18~
## $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,~
## $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,~
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,~
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,~

Basic dplyr Relocate

To use the relocate method, we can pass a dataset and the column name we would like to relocate. By default, the method will move the column to the front of our columns.

relocate(mtcars, wt)
##                        wt  mpg cyl  disp  hp drat  qsec vs am gear carb
## Mazda RX4           2.620 21.0   6 160.0 110 3.90 16.46  0  1    4    4
## Mazda RX4 Wag       2.875 21.0   6 160.0 110 3.90 17.02  0  1    4    4
## Datsun 710          2.320 22.8   4 108.0  93 3.85 18.61  1  1    4    1
## Hornet 4 Drive      3.215 21.4   6 258.0 110 3.08 19.44  1  0    3    1
## Hornet Sportabout   3.440 18.7   8 360.0 175 3.15 17.02  0  0    3    2
## Valiant             3.460 18.1   6 225.0 105 2.76 20.22  1  0    3    1
## Duster 360          3.570 14.3   8 360.0 245 3.21 15.84  0  0    3    4
## Merc 240D           3.190 24.4   4 146.7  62 3.69 20.00  1  0    4    2
## Merc 230            3.150 22.8   4 140.8  95 3.92 22.90  1  0    4    2
## Merc 280            3.440 19.2   6 167.6 123 3.92 18.30  1  0    4    4
## Merc 280C           3.440 17.8   6 167.6 123 3.92 18.90  1  0    4    4
## Merc 450SE          4.070 16.4   8 275.8 180 3.07 17.40  0  0    3    3
## Merc 450SL          3.730 17.3   8 275.8 180 3.07 17.60  0  0    3    3
## Merc 450SLC         3.780 15.2   8 275.8 180 3.07 18.00  0  0    3    3
## Cadillac Fleetwood  5.250 10.4   8 472.0 205 2.93 17.98  0  0    3    4
## Lincoln Continental 5.424 10.4   8 460.0 215 3.00 17.82  0  0    3    4
## Chrysler Imperial   5.345 14.7   8 440.0 230 3.23 17.42  0  0    3    4
## Fiat 128            2.200 32.4   4  78.7  66 4.08 19.47  1  1    4    1
## Honda Civic         1.615 30.4   4  75.7  52 4.93 18.52  1  1    4    2
## Toyota Corolla      1.835 33.9   4  71.1  65 4.22 19.90  1  1    4    1
## Toyota Corona       2.465 21.5   4 120.1  97 3.70 20.01  1  0    3    1
## Dodge Challenger    3.520 15.5   8 318.0 150 2.76 16.87  0  0    3    2
## AMC Javelin         3.435 15.2   8 304.0 150 3.15 17.30  0  0    3    2
## Camaro Z28          3.840 13.3   8 350.0 245 3.73 15.41  0  0    3    4
## Pontiac Firebird    3.845 19.2   8 400.0 175 3.08 17.05  0  0    3    2
## Fiat X1-9           1.935 27.3   4  79.0  66 4.08 18.90  1  1    4    1
## Porsche 914-2       2.140 26.0   4 120.3  91 4.43 16.70  0  1    5    2
## Lotus Europa        1.513 30.4   4  95.1 113 3.77 16.90  1  1    5    2
## Ford Pantera L      3.170 15.8   8 351.0 264 4.22 14.50  0  1    5    4
## Ferrari Dino        2.770 19.7   6 145.0 175 3.62 15.50  0  1    5    6
## Maserati Bora       3.570 15.0   8 301.0 335 3.54 14.60  0  1    5    8
## Volvo 142E          2.780 21.4   4 121.0 109 4.11 18.60  1  1    4    2

When working with dplyr and the tidyverse, we often use the pipe, %>% operator. With this, we can send the data set to our method to use. Here is a rewrite of the code above.

mtcars %>% relocate(wt)
##                        wt  mpg cyl  disp  hp drat  qsec vs am gear carb
## Mazda RX4           2.620 21.0   6 160.0 110 3.90 16.46  0  1    4    4
## Mazda RX4 Wag       2.875 21.0   6 160.0 110 3.90 17.02  0  1    4    4
## Datsun 710          2.320 22.8   4 108.0  93 3.85 18.61  1  1    4    1
## Hornet 4 Drive      3.215 21.4   6 258.0 110 3.08 19.44  1  0    3    1
## Hornet Sportabout   3.440 18.7   8 360.0 175 3.15 17.02  0  0    3    2
## Valiant             3.460 18.1   6 225.0 105 2.76 20.22  1  0    3    1
## Duster 360          3.570 14.3   8 360.0 245 3.21 15.84  0  0    3    4
## Merc 240D           3.190 24.4   4 146.7  62 3.69 20.00  1  0    4    2
## Merc 230            3.150 22.8   4 140.8  95 3.92 22.90  1  0    4    2
## Merc 280            3.440 19.2   6 167.6 123 3.92 18.30  1  0    4    4
## Merc 280C           3.440 17.8   6 167.6 123 3.92 18.90  1  0    4    4
## Merc 450SE          4.070 16.4   8 275.8 180 3.07 17.40  0  0    3    3
## Merc 450SL          3.730 17.3   8 275.8 180 3.07 17.60  0  0    3    3
## Merc 450SLC         3.780 15.2   8 275.8 180 3.07 18.00  0  0    3    3
## Cadillac Fleetwood  5.250 10.4   8 472.0 205 2.93 17.98  0  0    3    4
## Lincoln Continental 5.424 10.4   8 460.0 215 3.00 17.82  0  0    3    4
## Chrysler Imperial   5.345 14.7   8 440.0 230 3.23 17.42  0  0    3    4
## Fiat 128            2.200 32.4   4  78.7  66 4.08 19.47  1  1    4    1
## Honda Civic         1.615 30.4   4  75.7  52 4.93 18.52  1  1    4    2
## Toyota Corolla      1.835 33.9   4  71.1  65 4.22 19.90  1  1    4    1
## Toyota Corona       2.465 21.5   4 120.1  97 3.70 20.01  1  0    3    1
## Dodge Challenger    3.520 15.5   8 318.0 150 2.76 16.87  0  0    3    2
## AMC Javelin         3.435 15.2   8 304.0 150 3.15 17.30  0  0    3    2
## Camaro Z28          3.840 13.3   8 350.0 245 3.73 15.41  0  0    3    4
## Pontiac Firebird    3.845 19.2   8 400.0 175 3.08 17.05  0  0    3    2
## Fiat X1-9           1.935 27.3   4  79.0  66 4.08 18.90  1  1    4    1
## Porsche 914-2       2.140 26.0   4 120.3  91 4.43 16.70  0  1    5    2
## Lotus Europa        1.513 30.4   4  95.1 113 3.77 16.90  1  1    5    2
## Ford Pantera L      3.170 15.8   8 351.0 264 4.22 14.50  0  1    5    4
## Ferrari Dino        2.770 19.7   6 145.0 175 3.62 15.50  0  1    5    6
## Maserati Bora       3.570 15.0   8 301.0 335 3.54 14.60  0  1    5    8
## Volvo 142E          2.780 21.4   4 121.0 109 4.11 18.60  1  1    4    2

Using the After Parameter

If we would like to get more specific, we can use the .after parameter. To use this, we first pass the column we want to move, followed by the .after parameter and the column we would like to have before our specified column.

In this example, we can see that the disp column is not relocated to after the hp column.

mtcars %>% relocate(disp, .after = hp)
##                      mpg cyl  hp  disp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 110 160.0 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 110 160.0 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4  93 108.0 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 110 258.0 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 175 360.0 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 105 225.0 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 245 360.0 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4  62 146.7 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4  95 140.8 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 123 167.6 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 123 167.6 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 180 275.8 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 180 275.8 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 180 275.8 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 205 472.0 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 215 460.0 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 230 440.0 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  66  78.7 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  52  75.7 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  65  71.1 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4  97 120.1 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 150 318.0 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 150 304.0 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 245 350.0 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 175 400.0 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  66  79.0 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4  91 120.3 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4 113  95.1 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 264 351.0 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 175 145.0 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 335 301.0 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 109 121.0 4.11 2.780 18.60  1  1    4    2

Using the before paramter

Similar to the above, we can use the before parameter instead of after to put a column before another. In this example, we move wt before hp.

mtcars %>% relocate(wt, .before = hp)
##                      mpg cyl  disp    wt  hp drat  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 2.620 110 3.90 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 2.875 110 3.90 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0 2.320  93 3.85 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 3.215 110 3.08 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 3.440 175 3.15 17.02  0  0    3    2
## Valiant             18.1   6 225.0 3.460 105 2.76 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 3.570 245 3.21 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7 3.190  62 3.69 20.00  1  0    4    2
## Merc 230            22.8   4 140.8 3.150  95 3.92 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 3.440 123 3.92 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 3.440 123 3.92 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 4.070 180 3.07 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 3.730 180 3.07 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 3.780 180 3.07 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 5.250 205 2.93 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 5.424 215 3.00 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 5.345 230 3.23 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7 2.200  66 4.08 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7 1.615  52 4.93 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1 1.835  65 4.22 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1 2.465  97 3.70 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 3.520 150 2.76 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 3.435 150 3.15 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 3.840 245 3.73 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 3.845 175 3.08 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0 1.935  66 4.08 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3 2.140  91 4.43 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 1.513 113 3.77 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 3.170 264 4.22 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 2.770 175 3.62 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 3.570 335 3.54 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 2.780 109 4.11 18.60  1  1    4    2

Renaming Columns

We can also change the name with the relocate function by specifying a new name parameter. Although, there is the rename method in dplyr

mtcars %>% relocate(horsepower = hp)
##                     horsepower  mpg cyl  disp drat    wt  qsec vs am gear carb
## Mazda RX4                  110 21.0   6 160.0 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag              110 21.0   6 160.0 3.90 2.875 17.02  0  1    4    4
## Datsun 710                  93 22.8   4 108.0 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive             110 21.4   6 258.0 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout          175 18.7   8 360.0 3.15 3.440 17.02  0  0    3    2
## Valiant                    105 18.1   6 225.0 2.76 3.460 20.22  1  0    3    1
## Duster 360                 245 14.3   8 360.0 3.21 3.570 15.84  0  0    3    4
## Merc 240D                   62 24.4   4 146.7 3.69 3.190 20.00  1  0    4    2
## Merc 230                    95 22.8   4 140.8 3.92 3.150 22.90  1  0    4    2
## Merc 280                   123 19.2   6 167.6 3.92 3.440 18.30  1  0    4    4
## Merc 280C                  123 17.8   6 167.6 3.92 3.440 18.90  1  0    4    4
## Merc 450SE                 180 16.4   8 275.8 3.07 4.070 17.40  0  0    3    3
## Merc 450SL                 180 17.3   8 275.8 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC                180 15.2   8 275.8 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood         205 10.4   8 472.0 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental        215 10.4   8 460.0 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial          230 14.7   8 440.0 3.23 5.345 17.42  0  0    3    4
## Fiat 128                    66 32.4   4  78.7 4.08 2.200 19.47  1  1    4    1
## Honda Civic                 52 30.4   4  75.7 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla              65 33.9   4  71.1 4.22 1.835 19.90  1  1    4    1
## Toyota Corona               97 21.5   4 120.1 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger           150 15.5   8 318.0 2.76 3.520 16.87  0  0    3    2
## AMC Javelin                150 15.2   8 304.0 3.15 3.435 17.30  0  0    3    2
## Camaro Z28                 245 13.3   8 350.0 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird           175 19.2   8 400.0 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9                   66 27.3   4  79.0 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2               91 26.0   4 120.3 4.43 2.140 16.70  0  1    5    2
## Lotus Europa               113 30.4   4  95.1 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L             264 15.8   8 351.0 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino               175 19.7   6 145.0 3.62 2.770 15.50  0  1    5    6
## Maserati Bora              335 15.0   8 301.0 3.54 3.570 14.60  0  1    5    8
## Volvo 142E                 109 21.4   4 121.0 4.11 2.780 18.60  1  1    4    2

Using Select Helpers

We can also use the select helpers when relocationg. A list of those methods are here: https://tidyselect.r-lib.org/reference/language.html. Let’s see some examples.

We can use last_col with our relocate methods.

mtcars %>% relocate(hp, .after = last_col())
##                      mpg cyl  disp drat    wt  qsec vs am gear carb  hp
## Mazda RX4           21.0   6 160.0 3.90 2.620 16.46  0  1    4    4 110
## Mazda RX4 Wag       21.0   6 160.0 3.90 2.875 17.02  0  1    4    4 110
## Datsun 710          22.8   4 108.0 3.85 2.320 18.61  1  1    4    1  93
## Hornet 4 Drive      21.4   6 258.0 3.08 3.215 19.44  1  0    3    1 110
## Hornet Sportabout   18.7   8 360.0 3.15 3.440 17.02  0  0    3    2 175
## Valiant             18.1   6 225.0 2.76 3.460 20.22  1  0    3    1 105
## Duster 360          14.3   8 360.0 3.21 3.570 15.84  0  0    3    4 245
## Merc 240D           24.4   4 146.7 3.69 3.190 20.00  1  0    4    2  62
## Merc 230            22.8   4 140.8 3.92 3.150 22.90  1  0    4    2  95
## Merc 280            19.2   6 167.6 3.92 3.440 18.30  1  0    4    4 123
## Merc 280C           17.8   6 167.6 3.92 3.440 18.90  1  0    4    4 123
## Merc 450SE          16.4   8 275.8 3.07 4.070 17.40  0  0    3    3 180
## Merc 450SL          17.3   8 275.8 3.07 3.730 17.60  0  0    3    3 180
## Merc 450SLC         15.2   8 275.8 3.07 3.780 18.00  0  0    3    3 180
## Cadillac Fleetwood  10.4   8 472.0 2.93 5.250 17.98  0  0    3    4 205
## Lincoln Continental 10.4   8 460.0 3.00 5.424 17.82  0  0    3    4 215
## Chrysler Imperial   14.7   8 440.0 3.23 5.345 17.42  0  0    3    4 230
## Fiat 128            32.4   4  78.7 4.08 2.200 19.47  1  1    4    1  66
## Honda Civic         30.4   4  75.7 4.93 1.615 18.52  1  1    4    2  52
## Toyota Corolla      33.9   4  71.1 4.22 1.835 19.90  1  1    4    1  65
## Toyota Corona       21.5   4 120.1 3.70 2.465 20.01  1  0    3    1  97
## Dodge Challenger    15.5   8 318.0 2.76 3.520 16.87  0  0    3    2 150
## AMC Javelin         15.2   8 304.0 3.15 3.435 17.30  0  0    3    2 150
## Camaro Z28          13.3   8 350.0 3.73 3.840 15.41  0  0    3    4 245
## Pontiac Firebird    19.2   8 400.0 3.08 3.845 17.05  0  0    3    2 175
## Fiat X1-9           27.3   4  79.0 4.08 1.935 18.90  1  1    4    1  66
## Porsche 914-2       26.0   4 120.3 4.43 2.140 16.70  0  1    5    2  91
## Lotus Europa        30.4   4  95.1 3.77 1.513 16.90  1  1    5    2 113
## Ford Pantera L      15.8   8 351.0 4.22 3.170 14.50  0  1    5    4 264
## Ferrari Dino        19.7   6 145.0 3.62 2.770 15.50  0  1    5    6 175
## Maserati Bora       15.0   8 301.0 3.54 3.570 14.60  0  1    5    8 335
## Volvo 142E          21.4   4 121.0 4.11 2.780 18.60  1  1    4    2 109

We can also move all numeric characters to the front.

mtcars %>% relocate(where(is.numeric))
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

We can do something similar to move all numeric to the end.

mtcars %>% relocate(where(is.numeric), .after = last_col())
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2