Bring a Column to the Last Position

to_position_n(data, col, n)

Arguments

data

A dataframe or tibble.

col

Target column.

See also

Examples

library(tidyverse) test_data <- tibble(A = sample(c(NA_integer_, 1:3), size = 10, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 10, replace = TRUE), C = sample(c(NA_integer_, 7:9), size = 10, replace = TRUE), D = sample(c(NA_integer_, 10:12), size = 10, replace = TRUE), E = sample(c(NA_integer_, 13:15), size = 10, replace = TRUE), ) # To Position 1 to_position_1(data = test_data, col = D)
#> # A tibble: 10 x 5 #> D A B C E #> <int> <int> <int> <int> <int> #> 1 11 1 4 7 14 #> 2 10 2 4 7 13 #> 3 11 3 6 9 NA #> 4 10 1 NA 9 NA #> 5 10 1 6 7 15 #> 6 NA 2 4 NA NA #> 7 10 1 5 8 15 #> 8 11 2 5 NA 13 #> 9 11 1 5 9 14 #> 10 11 NA 5 7 15
# To Last Position to_last_position(data = test_data, col = B)
#> # A tibble: 10 x 5 #> A C D E B #> <int> <int> <int> <int> <int> #> 1 1 7 11 14 4 #> 2 2 7 10 13 4 #> 3 3 9 11 NA 6 #> 4 1 9 10 NA NA #> 5 1 7 10 15 6 #> 6 2 NA NA NA 4 #> 7 1 8 10 15 5 #> 8 2 NA 11 13 5 #> 9 1 9 11 14 5 #> 10 NA 7 11 15 5
# To Last Position to_position_n(data = test_data, col = B, n = 3)
#> # A tibble: 10 x 5 #> A C B D E #> <int> <int> <int> <int> <int> #> 1 1 7 4 11 14 #> 2 2 7 4 10 13 #> 3 3 9 6 11 NA #> 4 1 9 NA 10 NA #> 5 1 7 6 10 15 #> 6 2 NA 4 NA NA #> 7 1 8 5 10 15 #> 8 2 NA 5 11 13 #> 9 1 9 5 11 14 #> 10 NA 7 5 11 15
to_position_n(data = test_data, col = B, n = 4)
#> # A tibble: 10 x 5 #> A C D B E #> <int> <int> <int> <int> <int> #> 1 1 7 11 4 14 #> 2 2 7 10 4 13 #> 3 3 9 11 6 NA #> 4 1 9 10 NA NA #> 5 1 7 10 6 15 #> 6 2 NA NA 4 NA #> 7 1 8 10 5 15 #> 8 2 NA 11 5 13 #> 9 1 9 11 5 14 #> 10 NA 7 11 5 15
to_position_n(data = test_data, col = B, n = 2)
#> # A tibble: 10 x 5 #> A B C D E #> <int> <int> <int> <int> <int> #> 1 1 4 7 11 14 #> 2 2 4 7 10 13 #> 3 3 6 9 11 NA #> 4 1 NA 9 10 NA #> 5 1 6 7 10 15 #> 6 2 4 NA NA NA #> 7 1 5 8 10 15 #> 8 2 5 NA 11 13 #> 9 1 5 9 11 14 #> 10 NA 5 7 11 15