Use `relocate_dt()` to change column positions, using the same syntax as `select_dt()`. Check similar function as `relocate` in dplyr.
relocate_dt(.data, ..., how = "first", where = NULL)
.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 |
A data.table with rearranged columns.
#> a b c d e f #> <num> <num> <num> <char> <char> <char> #> 1: 1 1 1 a a adf %>% relocate_dt(f)#> f a b c d e #> <char> <num> <num> <num> <char> <char> #> 1: a 1 1 1 a adf %>% relocate_dt(a,how = "last")#> b c d e f a #> <num> <num> <char> <char> <char> <num> #> 1: 1 1 a a a 1df %>% relocate_dt(is.character)#> d e f a b c #> <char> <char> <char> <num> <num> <num> #> 1: a a a 1 1 1df %>% relocate_dt(is.numeric, how = "last")#> d e f a b c #> <char> <char> <char> <num> <num> <num> #> 1: a a a 1 1 1df %>% relocate_dt("[aeiou]")#> a e b c d f #> <num> <char> <num> <num> <char> <char> #> 1: 1 a 1 1 a adf %>% 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 1df %>% 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 adf %>% 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 adf %>% 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 adf2 <- 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 1df2 %>% relocate_dt(is.numeric, how="before", where = is.character)#> a c b d #> <num> <num> <char> <char> #> 1: 1 1 a a