R中的字符串分割操作 [英] string split operation in R
问题描述
在我的数据中,我有一串字符串.每个字符串有五个字符长.我想弄清楚如何分割字符串,以便保留前两个字符,后两个字符而忽略中间或第三个字符.
In my data I have a column of strings. Each string is five characters long. I would like to figure out how to split the string so that I keep the first two characters, the last two and disregard the middle or third character.
我查看了其他stackoverflow问题,发现下面列出的答案很有帮助.最初,下面的解决方案很有用,直到我看到在某些情况下它无法正常工作或以我未曾期望的方式工作.
I looked at other stackoverflow questions and found the answer listed below as helpful. Initially, the solution below was useful until I saw that in certain cases it didn't work or it worked in the way I wasn't expecting.
这就是我所拥有的:
statecensusFIPS <- c("01001", "03001", "13144")
newFIPS <- lapply(2:3, function(i){
if(i==2){
str_sub(statecensusFIPS, end = i)
} else {
str_sub(statecensusFIPS, i)
}})
StateFIPS <- newFIPS[[1]]
CountyFIPS <- newFIPS[[2]]
# Results
> StateFIPS
[1] "01" "03" "13"
> CountyFIPS
[1] "001" "001" "144"
如何调整代码,以便获得这些结果?
How do I adjust the code so that I have these results instead?
StateFIPS
[1] "01" "03" "13"
CountyFIPS
[1] "01" "01" "44"
推荐答案
这是怎么回事(假设您想将前2个字符用作状态字符,并将字符串的后2个字符用作县字符,并且所有字符串的长度均为5)?
How about this (assuming that you want first 2 characters as the statefips and last 2 characters of your strings as county fips and all your strings are of length 5)?
statecensusFIPS<-c("01001", "03001", "13144")
newFIPS<-lapply(2:3,function(i) if(i==2) str_sub(statecensusFIPS,end=i) else str_sub(statecensusFIPS,i+1))
StateFIPS<-newFIPS[[1]]
CountyFIPS<-newFIPS[[2]]
更简单的方法可能是:
statecensusFIPS<-c("01001", "03001", "13144")
stateFIPS<- str_sub(statecensusFIPS,end=2)
CountyFIPS<- str_sub(statecensusFIPS,4)
这篇关于R中的字符串分割操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!