2017-07-20 26 views

回答

2

实施例从基站-R:

gsub(".* (\\C).*", "\\1", a, perl = TRUE) 
[1] "S" "A" 
2

拆分,然后使用substr来提取第一字符

sapply(strsplit(a, " "), function(y) substr(x = y[2], start = 1, stop = 1)) 
#[1] "S" "A" 

OR

sapply(a, function(x) substr(unlist(strsplit(x, " "))[2], 1, 1)) 
#United States South America 
#   "S"   "A" 

或使用stringr

library(stringr) 
substr(x = word(string = a, start = 2, sep = " "), start = 1, stop = 1) 
#[1] "S" "A" 

word功能或使用的stringrstr_extract

library(stringr) 
str_extract(string = a, pattern = "(?<=\\s).") 
#[1] "S" "A" 
相关问题