I'm having a Thursday morning mental block, any suggestions on the following would be most appreciated... I have (as an example) surgery = c("d48", "d67", "dnc37", "a75", "d10", "a78", "d31", "d55", "d1") before each number part the possibilities are c("a", "d", "dnc"), I'm trying to split each element in "surgery" so that I have, status time d 48 d 67 dnc 37 a 75 d 10 a 78 d 31 d 55 d 1 I've tried various strsplit approaches but nothing has done what I need. thanks in advance Gary [[alternative HTML version deleted]]
one way is the following: data.frame(status = gsub("[0-9]", "", surgery), time = gsub("[a-z]", "", surgery)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Gary Collins" <collins.gs at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Thursday, August 23, 2007 10:03 AM Subject: [R] Splitting strings> I'm having a Thursday morning mental block, any suggestions on the > following > would be most appreciated... > > I have (as an example) > > surgery = c("d48", "d67", "dnc37", "a75", "d10", "a78", "d31", > "d55", "d1") > > before each number part the possibilities are c("a", "d", "dnc"), > I'm trying > to split each element in "surgery" so that I have, > > status time > d 48 > d 67 > dnc 37 > a 75 > d 10 > a 78 > d 31 > d 55 > d 1 > > I've tried various strsplit approaches but nothing has done what I > need. > > thanks in advance > > Gary > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
This applies the indicated perl-style regular expression where the first backreference (\\D+) is the non-digits and the second backreference (\\d+) is the digits. The two backreferences, but not the entire matched pattern itself, are passed as arguments x and y to the function whose body is the right hand side of the formula in the third argument. That is then simplified using rbind to give the result. library(gsubfn) strapply(surgery, "(\\D+)(\\d+)", ~ list(lets = x, nums = as.numeric(y)), backref = -2, perl = TRUE, simplify = rbind) More on gsubfn at http://gsubfn.googlecode.com On 8/23/07, Gary Collins <collins.gs at gmail.com> wrote:> I'm having a Thursday morning mental block, any suggestions on the following > would be most appreciated... > > I have (as an example) > > surgery = c("d48", "d67", "dnc37", "a75", "d10", "a78", "d31", > "d55", "d1") > > before each number part the possibilities are c("a", "d", "dnc"), I'm trying > to split each element in "surgery" so that I have, > > status time > d 48 > d 67 > dnc 37 > a 75 > d 10 > a 78 > d 31 > d 55 > d 1 > > I've tried various strsplit approaches but nothing has done what I need. > > thanks in advance > > Gary > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >