Move a column or columns to a new position

relocate(.df, ..., .before = NULL, .after = NULL)

Arguments

.df

A data.frame or data.table

...

A selection of columns to move. tidyselect compatible.

.before

Column to move selection before

.after

Column to move selection after

Examples

df <- data.table(
  a = 1:3,
  b = 1:3,
  c = c("a", "a", "b"),
  d = c("a", "a", "b")
)

df %>%
  relocate(c, .before = b)
#> # A tidytable: 3 × 4
#>       a c         b d    
#>   <int> <chr> <int> <chr>
#> 1     1 a         1 a    
#> 2     2 a         2 a    
#> 3     3 b         3 b    

df %>%
  relocate(a, b, .after = c)
#> # A tidytable: 3 × 4
#>   c         a     b d    
#>   <chr> <int> <int> <chr>
#> 1 a         1     1 a    
#> 2 a         2     2 a    
#> 3 b         3     3 b    

df %>%
  relocate(where(is.numeric), .after = c)
#> # A tidytable: 3 × 4
#>   c         a     b d    
#>   <chr> <int> <int> <chr>
#> 1 a         1     1 a    
#> 2 a         2     2 a    
#> 3 b         3     3 b