group character vector by a regex pattern
Examples
v <- c(
stringr::str_c("A", c(1, 2, 9, 10, 11, 12, 99, 101, 102)),
stringr::str_c("B", c(1, 2, 9, 10, 21, 32, 99, 101, 102))
) %>% sample()
group_vector(v)
#> $A
#> [1] "A12" "A11" "A1" "A9" "A10" "A102" "A2" "A99" "A101"
#>
#> $B
#> [1] "B99" "B1" "B2" "B21" "B102" "B9" "B32" "B10" "B101"
#>
group_vector(v, pattern = "\\w\\d")
#> $A1
#> [1] "A12" "A11" "A1" "A10" "A102" "A101"
#>
#> $A2
#> [1] "A2"
#>
#> $A9
#> [1] "A9" "A99"
#>
#> $B1
#> [1] "B1" "B102" "B10" "B101"
#>
#> $B2
#> [1] "B2" "B21"
#>
#> $B3
#> [1] "B32"
#>
#> $B9
#> [1] "B99" "B9"
#>
group_vector(v, pattern = "\\w(\\d)")
#> $`1`
#> [1] "A12" "A11" "A1" "B1" "A10" "A102" "B102" "B10" "B101" "A101"
#>
#> $`2`
#> [1] "B2" "B21" "A2"
#>
#> $`3`
#> [1] "B32"
#>
#> $`9`
#> [1] "B99" "A9" "B9" "A99"
#>
# unmatched part will alse be stored
group_vector(v, pattern = "\\d{2}")
#> $`10`
#> [1] "A10" "A102" "B102" "B10" "B101" "A101"
#>
#> $`11`
#> [1] "A11"
#>
#> $`12`
#> [1] "A12"
#>
#> $`21`
#> [1] "B21"
#>
#> $`32`
#> [1] "B32"
#>
#> $`99`
#> [1] "B99" "A99"
#>
#> $unmatch
#> [1] "A1" "B1" "A9" "B2" "B9" "A2"
#>