Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

relocate

Change column order


Description

Use 'relocate()' to change column positions, using the same syntax as 'select()'. Check similar function as 'relocate()' in dplyr.

Usage

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

Arguments

.data

A data.table

...

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.

Details

Once you relocate the columns, the order changes forever.

Value

A data.table with rearranged columns.

Examples

df <- data.table(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df
df %>% relocate(f)
df %>% relocate(a,how = "last")

df %>% relocate(is.character)
df %>% relocate(is.numeric, how = "last")
df %>% relocate("[aeiou]")

df %>% relocate(a, how = "after",where = f)
df %>% relocate(f, how = "before",where = a)
df %>% relocate(f, how = "before",where = c)
df %>% relocate(f, how = "after",where = c)

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

tidyft

Tidy Verbs for Fast Data Operations by Reference

v0.4.5
MIT + file LICENSE
Authors
Tian-Yuan Huang [aut, cre] (<https://orcid.org/0000-0002-3591-4203>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.