Bring a Column to Position 1

to_position_1(data, col)

Arguments

data

A dataframe or tibble.

col

Target column.

See also

select,reexports

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 12 NA 4 9 13 #> 2 11 3 4 7 15 #> 3 NA 3 5 8 15 #> 4 NA 3 4 7 NA #> 5 10 3 6 8 NA #> 6 11 1 5 9 14 #> 7 10 2 4 8 13 #> 8 NA 3 6 8 15 #> 9 NA 2 NA 9 NA #> 10 12 2 6 9 14
# 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 NA 9 12 13 4 #> 2 3 7 11 15 4 #> 3 3 8 NA 15 5 #> 4 3 7 NA NA 4 #> 5 3 8 10 NA 6 #> 6 1 9 11 14 5 #> 7 2 8 10 13 4 #> 8 3 8 NA 15 6 #> 9 2 9 NA NA NA #> 10 2 9 12 14 6
# 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 NA 9 4 12 13 #> 2 3 7 4 11 15 #> 3 3 8 5 NA 15 #> 4 3 7 4 NA NA #> 5 3 8 6 10 NA #> 6 1 9 5 11 14 #> 7 2 8 4 10 13 #> 8 3 8 6 NA 15 #> 9 2 9 NA NA NA #> 10 2 9 6 12 14
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 NA 9 12 4 13 #> 2 3 7 11 4 15 #> 3 3 8 NA 5 15 #> 4 3 7 NA 4 NA #> 5 3 8 10 6 NA #> 6 1 9 11 5 14 #> 7 2 8 10 4 13 #> 8 3 8 NA 6 15 #> 9 2 9 NA NA NA #> 10 2 9 12 6 14
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 NA 4 9 12 13 #> 2 3 4 7 11 15 #> 3 3 5 8 NA 15 #> 4 3 4 7 NA NA #> 5 3 6 8 10 NA #> 6 1 5 9 11 14 #> 7 2 4 8 10 13 #> 8 3 6 8 NA 15 #> 9 2 NA 9 NA NA #> 10 2 6 9 12 14