Skip to content
Back to formatted view

Raw Message

Message-ID: <43297EC0.7080305@lancaster.ac.uk>
Date: 2005-09-15T14:01:36Z
From: Barry Rowlingson
Subject: Splitting the string at the last sub-string
In-Reply-To: <Pine.LNX.4.61.0509151425340.2920@gannet.stats>

Prof Brian Ripley wrote:

>>substring(str, c(1, 26), c(25,length(str)))

  nchar(str) surely?

  regexps can be rather slow though. Here's two functions:

byRipley =
function(str,sub){
   lp=attr(regexpr(paste(".*",sub,sep=""),str),'match.length')
   return(substring(str, c(1, lp+1), c(lp,nchar(str))))
}

byJarek =
function(str,sub){
   y = unlist(strsplit(str,sub))
   return(cbind(paste(y[-length(y)], sub,  sep="", collapse = ""), 
y[length(y)]))
}

  and a quick test:

 > system.time(for(i in 1:100000){byJarek(str,sub)})
[1] 15.55  0.10 16.06  0.00  0.00

 > system.time(for(i in 1:100000){byRipley(str,sub)})
[1] 30.28  0.07 31.86  0.00  0.00

Baz