2017-07-20 19 views

答えて

2

stringrの例を用いて基地-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機能を使用するかからstr_extract

library(stringr) 
str_extract(string = a, pattern = "(?<=\\s).") 
#[1] "S" "A" 
関連する問題