Efficiently extract several elements from a string. See str_elem()
for
extracting single elements. This function is vectorized over the first
argument.
str_elems(string, indices, byrow = TRUE)
A character vector.
A vector of integerish values. Negative indexing is allowed as
in stringr::str_sub()
.
Should the elements be organised in the matrix with one row per
string (byrow = TRUE
, the default) or one column per string (byrow = FALSE
). See examples if you don't understand.
A character matrix.
Other single element extractors:
str_elem()
,
str_paste_elems()
string <- c("abc", "def", "ghi", "vwxyz")
str_elems(string, 1:2)
#> [,1] [,2]
#> [1,] "a" "b"
#> [2,] "d" "e"
#> [3,] "g" "h"
#> [4,] "v" "w"
str_elems(string, 1:2, byrow = FALSE)
#> [,1] [,2] [,3] [,4]
#> [1,] "a" "d" "g" "v"
#> [2,] "b" "e" "h" "w"
str_elems(string, c(1, 2, 3, 4, -1))
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] "a" "b" "c" "" "c"
#> [2,] "d" "e" "f" "" "f"
#> [3,] "g" "h" "i" "" "i"
#> [4,] "v" "w" "x" "y" "z"