Skip to content
Prev 163753 / 398506 Next

Strplit code

Dear Wacek,

"Wrong" is a bit strong, I think -- limited to single-pattern characters is
more accurate. Moreover, it isn't hard to make the function work with
multiple-character matches as well:

Strsplit <- function(x, split){
    if (length(x) > 1) {
        return(lapply(x, Strsplit, split))  # vectorization
        }
    result <- character(0)
    if (nchar(x) == 0) return(result)
    posn <- regexpr(split, x)
    if (posn <= 0) return(x)
    c(result, substring(x, 1, posn - 1), 
        Recall(substring(x, posn + attr(posn, "match.length"), 
          nchar(x)), split))  # recursion
    }

On the other hand, your function is much more efficient.

Regards,
 John 

------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox