Change the position of columns, using the same syntax as `select_dt()`. Check similar function as `relocate` in dplyr.

relocate_dt(.data, ..., how = "first", where = NULL)

Arguments

.data

A data.frame

...

Columns to move

how

The mode of movement, including "first","last","after","before". Default uses "first".

where

Destination of columns selected by .... Applicable for "after" and "before" mode.

Value

A data.table with rearranged columns.

See also

Examples

df <- data.table(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df
#>        a     b     c      d      e      f
#>    <num> <num> <num> <char> <char> <char>
#> 1:     1     1     1      a      a      a
df %>% relocate_dt(f)
#>         f     a     b     c      d      e
#>    <char> <num> <num> <num> <char> <char>
#> 1:      a     1     1     1      a      a
df %>% relocate_dt(a,how = "last")
#>        b     c      d      e      f     a
#>    <num> <num> <char> <char> <char> <num>
#> 1:     1     1      a      a      a     1

df %>% relocate_dt(is.character)
#>         d      e      f     a     b     c
#>    <char> <char> <char> <num> <num> <num>
#> 1:      a      a      a     1     1     1
df %>% relocate_dt(is.numeric, how = "last")
#>         d      e      f     a     b     c
#>    <char> <char> <char> <num> <num> <num>
#> 1:      a      a      a     1     1     1
df %>% relocate_dt("[aeiou]")
#>        a      e     b     c      d      f
#>    <num> <char> <num> <num> <char> <char>
#> 1:     1      a     1     1      a      a

df %>% relocate_dt(a, how = "after",where = f)
#>        b     c      d      e      f     a
#>    <num> <num> <char> <char> <char> <num>
#> 1:     1     1      a      a      a     1
df %>% relocate_dt(f, how = "before",where = a)
#>         f     a     b     c      d      e
#>    <char> <num> <num> <num> <char> <char>
#> 1:      a     1     1     1      a      a
df %>% relocate_dt(f, how = "before",where = c)
#>        a     b      f     c      d      e
#>    <num> <num> <char> <num> <char> <char>
#> 1:     1     1      a     1      a      a
df %>% relocate_dt(f, how = "after",where = c)
#>        a     b     c      f      d      e
#>    <num> <num> <num> <char> <char> <char>
#> 1:     1     1     1      a      a      a

df2 <- data.table(a = 1, b = "a", c = 1, d = "a")
df2 %>% relocate_dt(is.numeric,
                    how = "after",
                    where = is.character)
#>         b      d     a     c
#>    <char> <char> <num> <num>
#> 1:      a      a     1     1
df2 %>% relocate_dt(is.numeric,
                    how="before",
                    where = is.character)
#>        a     c      b      d
#>    <num> <num> <char> <char>
#> 1:     1     1      a      a