Skip to main content

dplyr::relocate

dplyr::relocate は、tidyverse コレクションの dplyr パッケージに含まれる関数で、列を移動するために使用される。

クイックリファレンス

library(tidyverse)

df %>%
relocate(col1, .after = col2)
penguins %>%
relocate(species, island, .after = sex)

# # A tibble: 344 × 7
# bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex species island
# <dbl> <dbl> <int> <int> <fct> <fct> <fct>
# 1 39.1 18.7 181 3750 male Adelie Torgersen
# 2 39.5 17.4 186 3800 female Adelie Torgersen
# 3 40.3 18 195 3250 female Adelie Torgersen
# 4 NA NA NA NA NA Adelie Torgersen
# 5 36.7 19.3 193 3450 female Adelie Torgersen
# 6 39.3 20.6 190 3650 male Adelie Torgersen
# 7 38.9 17.8 181 3625 female Adelie Torgersen
# 8 39.2 19.6 195 4675 male Adelie Torgersen
# 9 34.1 18.1 193 3475 NA Adelie Torgersen
# 10 42 20.2 190 4250 NA Adelie Torgersen
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

基本構文

relocate(.data, ..., .before = NULL, .after = NULL)
引数説明
.dataデータフレーム(または tibble)。
...移動する列名。<tidy-select> も指定可能。
.before指定された列名の前に移動する列名。.after との併用不可。<tidy-select> も指定可能。
.after指定された列名の後に移動する列名。.before との併用不可。<tidy-select> も指定可能。

使用例

1. 最初の位置に移動

penguins %>%
relocate(bill_length_mm, bill_depth_mm)

# # A tibble: 344 × 7
# bill_length_mm bill_depth_mm species island flipper_length_mm body_mass_g sex
# <dbl> <dbl> <fct> <fct> <int> <int> <fct>
# 1 39.1 18.7 Adelie Torgersen 181 3750 male
# 2 39.5 17.4 Adelie Torgersen 186 3800 female
# 3 40.3 18 Adelie Torgersen 195 3250 female
# 4 NA NA Adelie Torgersen NA NA NA
# 5 36.7 19.3 Adelie Torgersen 193 3450 female
# 6 39.3 20.6 Adelie Torgersen 190 3650 male
# 7 38.9 17.8 Adelie Torgersen 181 3625 female
# 8 39.2 19.6 Adelie Torgersen 195 4675 male
# 9 34.1 18.1 Adelie Torgersen 193 3475 NA
# 10 42 20.2 Adelie Torgersen 190 4250 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

2. 最後の位置に移動

penguins %>%
relocate(contains("mm"), .after = last_col())

# # A tibble: 344 × 7
# species island body_mass_g sex bill_length_mm bill_depth_mm flipper_length_mm
# <fct> <fct> <int> <fct> <dbl> <dbl> <int>
# 1 Adelie Torgersen 3750 male 39.1 18.7 181
# 2 Adelie Torgersen 3800 female 39.5 17.4 186
# 3 Adelie Torgersen 3250 female 40.3 18 195
# 4 Adelie Torgersen NA NA NA NA NA
# 5 Adelie Torgersen 3450 female 36.7 19.3 193
# 6 Adelie Torgersen 3650 male 39.3 20.6 190
# 7 Adelie Torgersen 3625 female 38.9 17.8 181
# 8 Adelie Torgersen 4675 male 39.2 19.6 195
# 9 Adelie Torgersen 3475 NA 34.1 18.1 193
# 10 Adelie Torgersen 4250 NA 42 20.2 190
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

3. 指定した列の前(または、後ろ)に移動

penguins %>%
relocate(where(is.numeric), .before = island)

# # A tibble: 344 × 7
# species bill_length_mm bill_depth_mm flipper_length_mm body_mass_g island sex
# <fct> <dbl> <dbl> <int> <int> <fct> <fct>
# 1 Adelie 39.1 18.7 181 3750 Torgersen male
# 2 Adelie 39.5 17.4 186 3800 Torgersen female
# 3 Adelie 40.3 18 195 3250 Torgersen female
# 4 Adelie NA NA NA NA Torgersen NA
# 5 Adelie 36.7 19.3 193 3450 Torgersen female
# 6 Adelie 39.3 20.6 190 3650 Torgersen male
# 7 Adelie 38.9 17.8 181 3625 Torgersen female
# 8 Adelie 39.2 19.6 195 4675 Torgersen male
# 9 Adelie 34.1 18.1 193 3475 Torgersen NA
# 10 Adelie 42 20.2 190 4250 Torgersen NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows
penguins %>%
relocate(where(is.numeric), .after = island)

# # A tibble: 344 × 7
# species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
# <fct> <fct> <dbl> <dbl> <int> <int> <fct>
# 1 Adelie Torgersen 39.1 18.7 181 3750 male
# 2 Adelie Torgersen 39.5 17.4 186 3800 female
# 3 Adelie Torgersen 40.3 18 195 3250 female
# 4 Adelie Torgersen NA NA NA NA NA
# 5 Adelie Torgersen 36.7 19.3 193 3450 female
# 6 Adelie Torgersen 39.3 20.6 190 3650 male
# 7 Adelie Torgersen 38.9 17.8 181 3625 female
# 8 Adelie Torgersen 39.2 19.6 195 4675 male
# 9 Adelie Torgersen 34.1 18.1 193 3475 NA
# 10 Adelie Torgersen 42 20.2 190 4250 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows