如果我有一个字符串,并希望拆分最后一个数字并保持拆分hpw的最后一部分,我可以这样做吗?
x <- c("ID",paste0("X",1:10,state.name[1:10]))
我想要
[1] NA "Alabama" "Alaska" "Arizona" "Arkansas" [6] "California" "Colorado" "Connecticut" "Delaware" "Florida" [11] "Georgia"
但是会满足于:
[1] "ID" "Alabama" "Alaska" "Arizona" "Arkansas" [6] "California" "Colorado" "Connecticut" "Delaware" "Florida" [11] "Georgia"
我可以通过以下方式得到第一部分:
unlist(strsplit(x,"[^0-9]*$"))
但是想要第二部分.
先感谢您.
library(stringr) unlist(lapply(str_split(x,"[0-9]"),tail,n=1))
给
[1] "ID" "Alabama" "Alaska" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" "Delaware" [10] "Florida" "Georgia"
我会查看文档字符串(最有可能)更好的方法.