Another alternative is to use capture sub-expressions with the regmatches functions regmatches and regexec .
# the original example x <- 'hello stackoverflow'
This returns the entire string, the first character and a pop-up result in a list of length 1.
myStrings [[1]] [1] "hello stackoverflow" "h" "ello stackoverflow"
which is equivalent to list(c(x, substr(x, 1, 1), substr(x, 2, nchar(x)))) . That is, it contains a super-set of desired elements, as well as a full line.
Adding sapply will allow this method to work for a character vector of length> 1.
# a slightly more interesting example xx <- c('hello stackoverflow', 'right back', 'at yah')
This returns a list with a matching full string as the first item and corresponding subexpressions marked with () as the following items. Thus, in the regular expression '(^.)(.*)' , (^.) Matches the first character, and (.*) Matches the remaining characters.
myStrings [[1]] [1] "hello stackoverflow" "h" "ello stackoverflow" [[2]] [1] "right back" "r" "ight back" [[3]] [1] "at yah" "a" "t yah"
Now we can use the trusty sapply + [ method to pull the necessary substrings.
myFirstStrings <- sapply(myStrings, "[", 2) myFirstStrings [1] "h" "r" "a" mySecondStrings <- sapply(myStrings, "[", 3) mySecondStrings [1] "ello stackoverflow" "ight back" "t yah"
lmo Jun 12 '17 at 19:51 on 2017-06-12 19:51
source share