Subsetting a string
Extract a substring from a string by picking up individual letters by their position.
A character vector, or an XString, XStringViews or MaskedXString object.
An integer vector with no NAs.
Unlike with the
i must contain valid positions.
A character vector of length 1 when
x is an XString
or MaskedXString object (the masks are ignored for the latter).
A character vector of the same length as
is a character vector or an XStringViews object.
Note that, because
i must contain valid positions,
all non-NA elements in the result are guaranteed to have exactly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
x <- c("abcd", "ABC") i <- c(3, 1, 1, 2, 1) ## With a character vector: letter(x, 3:1) letter(x, 3) letter(x, i) #letter(x, 4) # Error! ## With a BString object: letter(BString(x), i) # returns a character vector BString(x)[i] # returns a BString object ## With an XStringViews object: x2 <- as(BStringSet(x), "Views") letter(x2, i)