Hi, I have a vector of strings like: c("a1b1","a2b2","a1b2") which I want to spilt into two parts like: c("a1","a2","a2") and c("b1","b2,"b2"). So there is always a first part with a+number and a second part with b+number. Unfortunately there is no separator I could use to directly split the vectors.. Any idea how to handle such cases? /Johannes
Dear Johannes, May not be the best way, but this looks like what you described:> x <- c("a1b1","a2b2","a1b2") > x[1] "a1b1" "a2b2" "a1b2"> substr(x, 1, 2)[1] "a1" "a2" "a1"> substr(x, 3, 4)[1] "b1" "b2" "b2" HTH, Jorge.- On Wed, Mar 13, 2013 at 7:37 PM, Johannes Radinger <> wrote:> Hi, > > I have a vector of strings like: > c("a1b1","a2b2","a1b2") which I want to spilt into two parts like: > c("a1","a2","a2") and c("b1","b2,"b2"). So there is > always a first part with a+number and a second part with b+number. > Unfortunately there is no separator I could use to directly split > the vectors.. Any idea how to handle such cases? > > /Johannes > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thank you Jorge! thats working perfectly... /johannes On Wed, Mar 13, 2013 at 9:45 AM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:> Dear Johannes, > > May not be the best way, but this looks like what you described: > >> x <- c("a1b1","a2b2","a1b2") >> x > [1] "a1b1" "a2b2" "a1b2" >> substr(x, 1, 2) > [1] "a1" "a2" "a1" >> substr(x, 3, 4) > [1] "b1" "b2" "b2" > > HTH, > Jorge.- > > > On Wed, Mar 13, 2013 at 7:37 PM, Johannes Radinger <> wrote: >> >> Hi, >> >> I have a vector of strings like: >> c("a1b1","a2b2","a1b2") which I want to spilt into two parts like: >> c("a1","a2","a2") and c("b1","b2,"b2"). So there is >> always a first part with a+number and a second part with b+number. >> Unfortunately there is no separator I could use to directly split >> the vectors.. Any idea how to handle such cases? >> >> /Johannes >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > >
Hi, You could use: library(stringr) ?str_sub() lapply(2:3,function(i) if(i==2) str_sub(x,end=i) else str_sub(x,i)) #[[1]] #[1] "a1" "a2" "a1" #[[2]] #[1] "b1" "b2" "b2" A.K. ----- Original Message ----- From: Johannes Radinger <johannesradinger at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, March 13, 2013 4:37 AM Subject: [R] string split at xth position Hi, I have a vector of strings like: c("a1b1","a2b2","a1b2") which I want to spilt into two parts like: c("a1","a2","a2") and c("b1","b2,"b2"). So there is always a first part with a+number and a second part with b+number. Unfortunately there is no separator I could use to directly split the vectors.. Any idea how to handle such cases? /Johannes ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Here is another way require(stringr) aaa<-paste0("a", 1:20) bbb<-paste0("b", 101:120) ab<-paste0(aaa,bbb) ab ptrn<-"([ab][[:digit:]]*)" unlist(str_extract_all(ab, ptrn))> Hi, > > I have a vector of strings like: > c("a1b1","a2b2","a1b2") which I want to spilt into two parts like: > c("a1","a2","a2") and c("b1","b2,"b2"). So there is > always a first part with a+number and a second part with b+number. > Unfortunately there is no separator I could use to directly split > the vectors.. Any idea how to handle such cases? > > /Johannes--
Hi, If you have cases like these: ?x1<- c("a1b11","a10b2","a2b2","a140b31") ?lapply(list(c(1,2),c(3,4)),function(i) substr(x1,i[1],i[2])) #it will not work #[[1]] #[1] "a1" "a1" "a2" "a1" #[[2]] #[1] "b1" "0b" "b2" "40" let1<-unique(unlist(strsplit(gsub("\\d+","",x1),""))) ?split(unlist(strsplit(gsub("(\\w\\d+)(\\w\\d+)","\\1 \\2",x1)," ")),let1) #$a #[1] "a1"?? "a10"? "a2"?? "a140" #$b #[1] "b11" "b2"? "b2"? "b31" A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: Johannes Radinger <johannesradinger at gmail.com> Cc: Sent: Wednesday, March 13, 2013 8:32 AM Subject: Re: [R] string split at xth position Also, You could try: ?lapply(list(c(1,2),c(3,4)),function(i) substr(x,i[1],i[2])) #[[1]] #[1] "a1" "a2" "a1" #[[2]] #[1] "b1" "b2" "b2" A.K. ----- Original Message ----- From: Johannes Radinger <johannesradinger at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, March 13, 2013 4:37 AM Subject: [R] string split at xth position Hi, I have a vector of strings like: c("a1b1","a2b2","a1b2") which I want to spilt into two parts like: c("a1","a2","a2") and c("b1","b2,"b2"). So there is always a first part with a+number and a second part with b+number. Unfortunately there is no separator I could use to directly split the vectors.. Any idea how to handle such cases? /Johannes ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.