HAVE WANT1 WANT2
CLStephen Five CL Stephen Five
RTQQuent Lou X RTQ Quent Lou X
我们学校系统上存在数据输入错误,我有“HAVE”列,希望将其分成“WANT1”和“WANT2”
WANT1 = take the first n-1 CAPITAL letters
WANT2 = take the remaining letters
同时尝试stringr和基础R:
library(stringr)
str_remove(x, "[A-Z][^A-Z].+")
#[1] "CL" "RTQ"
str_extract(x, "[A-Z][^A-Z].+")
#[1] "Stephen Five" "Quent Lou X"
sub("[A-Z][^A-Z].+", "", x)
#[1] "CL" "RTQ"
sub("[A-Z]+([A-Z][^A-Z].+)", "\\1", x)
#[1] "Stephen Five" "Quent Lou X"