[R] Number of words in a string

Charilaos Skiadas cskiadas at gmail.com
Wed Apr 9 23:39:51 CEST 2008


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



More information about the R-help mailing list