relocate()1. Move Var3 to be ahead of
Var2
Review the data (d1)
# A tibble: 3 x 3
Var1 Var2 Var3
<chr> <dbl> <dbl>
1 a 2 3.6
2 b NA 8.5
3 c 3 NA
Move Var3 in front of Var2 by using the
.before argument
d1 %>%
dplyr::relocate(Var3, .before = Var2)
# A tibble: 3 x 3
Var1 Var3 Var2
<chr> <dbl> <dbl>
1 a 3.6 2
2 b 8.5 NA
3 c NA 3
2. Move Var2 and Var3 to be ahead
of Var1
Review the data (d1)
# A tibble: 3 x 3
Var1 Var2 Var3
<chr> <dbl> <dbl>
1 a 2 3.6
2 b NA 8.5
3 c 3 NA
Move Var2 and Var3 in front of
Var1
d1 %>%
dplyr::relocate(Var2, Var3)
# A tibble: 3 x 3
Var2 Var3 Var1
<dbl> <dbl> <chr>
1 2 3.6 a
2 NA 8.5 b
3 3 NA c
You would get the same result listing all variables
d1 %>%
dplyr::relocate(Var2, Var3, Var1)
# A tibble: 3 x 3
Var2 Var3 Var1
<dbl> <dbl> <chr>
1 2 3.6 a
2 NA 8.5 b
3 3 NA c
3. Move Var1 after
Var2
Review the data (d1)
# A tibble: 3 x 3
Var1 Var2 Var3
<chr> <dbl> <dbl>
1 a 2 3.6
2 b NA 8.5
3 c 3 NA
Move Var1 after Var2 by using the
.after argument
d1 %>%
dplyr::relocate(Var1, .after = Var2)
# A tibble: 3 x 3
Var2 Var1 Var3
<dbl> <chr> <dbl>
1 2 a 3.6
2 NA b 8.5
3 3 c NA
select()1. Move Var3 to the front
Review the data (d1)
# A tibble: 3 x 3
Var1 Var2 Var3
<chr> <dbl> <dbl>
1 a 2 3.6
2 b NA 8.5
3 c 3 NA
Move Var3 to the front using the
dplyr::select() function in addition to the
tidyselect selection helper everything().
d1 %>%
dplyr::select(Var3, tidyselect::everything())
# A tibble: 3 x 3
Var3 Var1 Var2
<dbl> <chr> <dbl>
1 3.6 a 2
2 8.5 b NA
3 NA c 3
Return to Reorder