Extract and replace subsequences from a bstr sequences

bstr_sub(bstrobj, start = 1L, end = -1L)

bstr_sub(bstrobj, start = 1L, end = -1L, omit_na = FALSE) <- value

bstr_sub_replace(..., replacement, value = replacement)

Arguments

bstrobj

bstr class object or character vector

start

start

end

end

omit_na

Single logical value. If `TRUE`, missing values in any of the arguments provided will result in an unchanged input.

value

replacement string

...

arguments to be passed to bstr_sub<-

replacement

alias of value [wherever applicable]

Examples

temp <- bstr_rand_seq(1, 10, seed = 1) c( temp, bstr_sub(temp, 1, 5), bstr_sub(temp, 5), bstr_sub(temp, end = 5), bstr_sub(temp, 1:2, c(6,2)) )
#> class: bstr,character #> number of sequences: 6 #> [1] no name 1 : NTdvKuxigD 10 #> [2] no name 1 : NTdvK 5 #> [3] no name 1 : KuxigD 6 #> [4] no name 1 : NTdvK 5 #> [5] no name 1 : NTdvKu 6 #> [6] <NA> : T 1
# Negative indices c( temp, bstr_sub(temp, -1), bstr_sub(temp, -7), bstr_sub(temp, end = -7) )
#> class: bstr,character #> number of sequences: 4 #> [1] no name 1 : NTdvKuxigD 10 #> [2] no name 1 : D 1 #> [3] no name 1 : vKuxigD 7 #> [4] no name 1 : NTdv 4
# Alternatively, you can pass in a two colum matrix, as in the # output from stringr::str_locate_all temp <- bstr("abcde fghij") pos <- stringr::str_locate_all(temp, "[aefi]")[[1]] bstr_sub(temp, pos)
#> class: bstr,character #> number of sequences: 4 #> [1] no name 1 : a 1 #> [2] <NA> : e 1 #> [3] <NA> : f 1 #> [4] <NA> : i 1
bstr_sub(temp, pos[, 1], pos[, 2])
#> class: bstr,character #> number of sequences: 4 #> [1] no name 1 : a 1 #> [2] <NA> : e 1 #> [3] <NA> : f 1 #> [4] <NA> : i 1
# Vectorisation bstr_sub(temp, seq_len(bstr_length(temp)))
#> class: bstr,character #> number of sequences: 11 #> [1] no name 1 : abcde fghij 11 #> [2] <NA> : bcde fghij 10 #> [3] <NA> : cde fghij 9 #> [4] <NA> : de fghij 8 #> [5] <NA> : e fghij 7 #> [6] <NA> : fghij 6
bstr_sub(temp, end = seq_len(bstr_length(temp)))
#> class: bstr,character #> number of sequences: 11 #> [1] no name 1 : a 1 #> [2] <NA> : ab 2 #> [3] <NA> : abc 3 #> [4] <NA> : abcd 4 #> [5] <NA> : abcde 5 #> [6] <NA> : abcde 6
# Replacement form x <- "BBCDEF" bstr_sub(x, 1, 1) <- "A"; x
#> class: bstr,character #> number of sequences: 1 #> [1] no name 1 : ABCDEF 6
bstr_sub(x, -1, -1) <- "K"; x
#> class: bstr,character #> number of sequences: 1 #> [1] no name 1 : ABCDEK 6
bstr_sub(x, -2, -2) <- "GHIJ"; x
#> class: bstr,character #> number of sequences: 1 #> [1] no name 1 : ABCDGHIJK 9
bstr_sub(x, 2, -2) <- ""; x
#> class: bstr,character #> number of sequences: 1 #> [1] no name 1 : AK 2
# If you want to keep the original if some argument is NA, # use omit_na = TRUE x1 <- x2 <- x3 <- x4 <- bstr("AAA") bstr_sub(x1, 1, NA) <- "B" bstr_sub(x2, 1, 2) <- NA bstr_sub(x3, 1, NA, omit_na = TRUE) <- "B" bstr_sub(x4, 1, 2, omit_na = TRUE) <- NA c(x1, x2, x3, x4)
#> class: bstr,character #> number of sequences: 4 #> [1] no name 1 : <NA> NA #> [2] no name 1 : <NA> NA #> [3] no name 1 : AAA NA #> [4] no name 1 : AAA NA