我想在空格后选择第一个字符。我的MWE是由
a <- c("United States", "South America")
a
"S" "A"
base-R的例子:
gsub(".* (\\C).*", "\\1", a, perl = TRUE)
[1] "S" "A"
inds = regexpr(" ", a) + 1
substring(a, inds, inds)
#[1] "S" "A"
或者使用str_extract
的stringr
library(stringr)
str_extract(string = a, pattern = "(?<=\\s).")
#[1] "S" "A"