Skip to content
Prev 163727 / 398506 Next

Strplit code

Dear Brian (and original poster),

My apologies -- I didn't notice the original posting.

By coincidence, I have a version of strsplit() that I've used to
illustrate recursion:

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+1, nchar(x)), split))  # recursion
    }

Illustration:
+              "Whether t'is nobler,in the mind",
+              "to suffer the slings:and arrows:of outrageous fortune",
+              "or to take arms;against;a sea of troubles")
[[1]]
[1] "To be"                "or not to be"         "that is the
question"

[[2]]
[1] "Whether t'is nobler" "in the mind"        

[[3]]
[1] "to suffer the slings"  "and arrows"            "of outrageous
fortune"

[[4]]
[1] "or to take arms"   "against"           "a sea of troubles"



I think that this should work in S-PLUS, though I haven't tried it.

Regards,
 John

On Wed, 3 Dec 2008 20:46:56 +0000 (GMT)
Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/