How to split everything after second whitespace char using regular expression? I want to remove A, B, C and D from these names: nam <- c("Smith John A", "Smith David B C", "Smith Ryan C D") Thanks, Johannes
Try this:> gsub("([a-z]*\\s[a-z]*).*", "\\1", nam)[1] "Smith John" "Smith David" "Smith Ryan" On Fri, Nov 6, 2009 at 4:11 PM, johannes rara <johannesraja at gmail.com> wrote:> How to split everything after second whitespace char using regular > expression? I want to remove A, B, C and D from these names: > > nam <- c("Smith John A", "Smith David B C", "Smith Ryan C D") > > Thanks, > Johannes > > ______________________________________________ > R-help at r-project.org 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. >
Gabor Grothendieck
2009-Nov-06 13:06 UTC
[R] How to strip everything after second whitespace?
Try this: library(gsubfn) strapply(nam, "^\\w+ \\w+", simplify = c) On Fri, Nov 6, 2009 at 3:11 AM, johannes rara <johannesraja at gmail.com> wrote:> How to split everything after second whitespace char using regular > expression? I want to remove A, B, C and D from these names: > > nam <- c("Smith John A", "Smith David B C", "Smith Ryan C D")