R语言如何字符串分割“中国123abc”成“中国”,“123”,“abc”?

不要说strsplit函数了,这个研究过了不可以做到。
其实,最终目的就是把一个字符串中的数字汉子字母分成子串。
求高手回答,不胜感激这个样子的?

想了一个比较笨的方法。先在R里定义这个函数:

split.string<-function(string){
    str2<-strsplit(string,"")[[1]]
    string.split<-NULL
    j<-1
    string.split[j]<-str2[1]
    find.type<-function(char){
        if(grepl("[[:alpha:]]",char))
            type<-"alphabet"
        else if(grepl("[[:digit:]]",char))
            type<-"digit"
        else type<-"chinese"
        type
    }
    type<-find.type(str2[1])
    for(i in 2:length(str2)){
        type2<-find.type(str2[i])
        if(type2==type) string.split[j]<-paste(string.split[j],str2[i],sep="")
        else{
            j<-j+1
            type<-type2
            string.split<-c(string.split,str2[i])
        }
    }
    string.split
}

直接跑

split.string("中国123abc")

就好了

如果要区分更多的东西,就改一下内部的find.type()函数。我现在写的只能区分字母、数字和其他东西(比如标点和中文就分不开了)。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-12-28

其实可以把分割理解成替换,用sub()函数就轻松解决。

a<-"中国123abc"
a<-sub("1"," 1",a)
a<-sub("3","3 ",a)
a


个人见解

第2个回答  2017-07-23

试试regular expression

# install.packages('stringr')
s = '中国123abc'
stringr::str_extract_all(s, '\\D+')
stringr::str_extract_all(s, '[0-9]+')

第3个回答  2017-08-26
载入新包
install.packages(stringr)
library(stringr)
a<-"中国123abc"
num <- str_count(a)
index <- str_locate_all(a, "(\\d){2,}")
result1 <- str_sub(a, 1, b[[1]][1]-1)
result2 <- str_sub(a, b[[1]][1], b[[1]][2])
result3 <- str_sub(a, b[[1]][2]+1, num)
相似回答