[R] Number of words in a string

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 9 18:01:57 CEST 2008


Actually the last example was all but the first word,
not all but the last word.  All but the last word is:

> sapply(strapply(C, "\\w+"), head, -1)
[[1]]
[1] "My"

[[2]]
[1] "Its"    "really"

[[3]]
character(0)


On Wed, Apr 9, 2008 at 11:56 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> See strapply in the gsubfn package and the gsubfn home page at
> gsubfn.googlecode.com.
> Here we extract the words, the last word and all but the last word:
>
> > library(gsubfn)
> > strapply(C, "\\w+")
> [[1]]
> [1] "My"  "Dog"
>
> [[2]]
> [1] "Its"    "really" "good"
>
> [[3]]
> [1] "Beautiful"
>
> > sapply(strapply(C, "\\w+"), tail, 1)
> [1] "Dog"       "good"      "Beautiful"
> > sapply(strapply(C, "\\w+"), tail, -1)
> [[1]]
> [1] "Dog"
>
> [[2]]
> [1] "really" "good"
>
> [[3]]
> character(0)
>
>
>
> On Wed, Apr 9, 2008 at 11:44 AM, Shubha Vishwanath Karanth
> <shubhak at ambaresearch.com> wrote:
> > Got all the answers using ?strsplit... Is there any way without using string split?... More specifically... How can I just extract the last word in all the strings without using ?strsplit ?
> >
> > Shubha Karanth | Amba Research
> > Ph +91 80 3980 8031 | Mob +91 94 4886 4510
> > Bangalore * Colombo * London * New York * San José * Singapore * www.ambaresearch.com
> >
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Hans-Joerg Bibiko
> > Sent: Wednesday, April 09, 2008 9:10 PM
> > To: r-help at stat.math.ethz.ch
> >
> > Subject: Re: [R] Number of words in a string
> >
> >
> > On 9 Apr 2008, at 17:29, Markus Gesmann wrote:
> > > Would this:
> > >
> > > sapply(strsplit(C, " "), length)
> > >
> > > work for?
> >
> > or
> >
> > length(unlist(strsplit(C, " ")))
> >
> > --Hans
> >
> > ______________________________________________
> > 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.
> > This e-mail may contain confidential and/or privileged i...{{dropped:10}}
> >
> >
> > ______________________________________________
> > 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.
> >
>



More information about the R-help mailing list