R stringr 解析大小写字母

问题描述 投票:0回答:1
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
r stringr
1个回答
0
投票

同时尝试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" 
© www.soinside.com 2019 - 2024. All rights reserved.