Re: [R] Number of words in a string

From: Hans-Jörg Bibiko <>
Date: Wed, 09 Apr 2008 19:27:26 +0200

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.


--Hans mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Apr 2008 - 17:30:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Apr 2008 - 22:30:29 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive