Bring a Column to the Last Position
to_last_position(data, col)
data | A dataframe or tibble. |
---|---|
col | Target column. |
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 12 1 NA 9 NA #> 2 12 2 6 7 NA #> 3 11 2 6 7 14 #> 4 11 1 6 8 15 #> 5 10 2 4 8 15 #> 6 10 1 6 7 13 #> 7 NA 3 5 7 NA #> 8 11 NA NA 7 13 #> 9 12 1 5 8 13 #> 10 11 2 NA NA NA# 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 9 12 NA NA #> 2 2 7 12 NA 6 #> 3 2 7 11 14 6 #> 4 1 8 11 15 6 #> 5 2 8 10 15 4 #> 6 1 7 10 13 6 #> 7 3 7 NA NA 5 #> 8 NA 7 11 13 NA #> 9 1 8 12 13 5 #> 10 2 NA 11 NA NA#> # A tibble: 10 x 5 #> A C B D E #> <int> <int> <int> <int> <int> #> 1 1 9 NA 12 NA #> 2 2 7 6 12 NA #> 3 2 7 6 11 14 #> 4 1 8 6 11 15 #> 5 2 8 4 10 15 #> 6 1 7 6 10 13 #> 7 3 7 5 NA NA #> 8 NA 7 NA 11 13 #> 9 1 8 5 12 13 #> 10 2 NA NA 11 NA#> # A tibble: 10 x 5 #> A C D B E #> <int> <int> <int> <int> <int> #> 1 1 9 12 NA NA #> 2 2 7 12 6 NA #> 3 2 7 11 6 14 #> 4 1 8 11 6 15 #> 5 2 8 10 4 15 #> 6 1 7 10 6 13 #> 7 3 7 NA 5 NA #> 8 NA 7 11 NA 13 #> 9 1 8 12 5 13 #> 10 2 NA 11 NA NA#> # A tibble: 10 x 5 #> A B C D E #> <int> <int> <int> <int> <int> #> 1 1 NA 9 12 NA #> 2 2 6 7 12 NA #> 3 2 6 7 11 14 #> 4 1 6 8 11 15 #> 5 2 4 8 10 15 #> 6 1 6 7 10 13 #> 7 3 5 7 NA NA #> 8 NA NA 7 11 13 #> 9 1 5 8 12 13 #> 10 2 NA NA 11 NA