[Rd] splitting strings efficiently

Mark Kimpel
Wed, 24 Sep 2008 16:12:19 -0000

I have a very long list of strings. Each string actually contains multiple
values separated by a semi-colon. I need to turn each string into a vector
of the values delimited by the semi-colons. I know I can do this very
laboriously by using loops, nchar, and substr, but it is terribly slow. Is
there a basic R function that handles this situation? If not, is there
perhaps a faster way to do it than I currently am, which is to lapply the
following function? Thanks, Mark

#######################################################################################
string.tokenizer.func<-function(string, separator){
  new.vec<- NULL
  newString<- ""
  if(is.null(string)) {new.vec<-""} else {
    for(i in 1:(nchar(string) + 1)){
      if(substr(string, i, i) == separator){
        new.vec<-c(new.vec,newString)
        newString <- ""
      } else {
        newString<-paste(newString, substr(string, i, i), sep="")
      }
    }
    new.vec<-c(new.vec,newString)
  }
  new.vec
}
------------------------------------------------------------
Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry
Indiana University School of Medicine

15032 Hunter Court, Westfield, IN 46074

(317) 490-5129 Work, & Mobile & VoiceMail
(317) 399-1219 Home
Skype: mkimpel

******************************************************************

	[[alternative HTML version deleted]]