[R] Number of words in a string

Shubha Vishwanath Karanth shubhak at ambaresearch.com
Thu Apr 10 07:43:54 CEST 2008

So powerful, the gsub... But I really don’t understand the how the regular expressions like " *\\S+$", need to be used and how to make best use of it... Any article/material/links that I can go through?

BR, Shubha
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 Charilaos Skiadas
Sent: Thursday, April 10, 2008 3:10 AM
To: Hans-Jörg Bibiko
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Number of words in a string

On Apr 9, 2008, at 1:27 PM, Hans-Jörg Bibiko wrote:

> On 09.04.2008, at 17:46, Shubha Vishwanath Karanth wrote:
>> To put it simple,
>> C=c("My Dog", "Its really good", "Beautiful")
>> Now,
>> SOMEFUNCTION(C) should give: c("My", "Its really", "")
> SOMEFUNCTION <- function(x) gsub(" *\\w+$", "", x)
> But be aware that this won't work for instance for combining  
> diacritics.
> If you have this:
> C <- c("My Dog", "Its really good", "Beautiful", "Tuli faŝda")
> in fasda above the s is a combining circumfix ^
> would give
> [1] "My"         "Its really" ""           "Tuli faŝ"
> Then one should use the strsplit approach.

How about:

SOMEFUNCTION <- function(x) gsub(" *\\S+$", "", x)

> Cheers,
> --Hans

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

R-help at r-project.org mailing list
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 information. If you are not the intended recipient (or have received this
e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of
the material in this e-mail is strictly forbidden.  Any views or opinions presented are solely those of the author and do not
necessarily represent those of Amba Holdings Inc., and/or its affiliates.  Important additional terms relating to this email can be obtained
at  http://www.ambaresearch.com/disclaimer

More information about the R-help mailing list