[R] clipping off words inside a vector of strings

Romain Francois rfrancois at mango-solutions.com
Mon Oct 15 18:08:27 CEST 2007


Lemon Curry ?

sapply( strsplit( monty, " " ), function(x) {
   paste( substring(x,1,3), collapse = " " )
})

is a way to do it, ...
There is probably a better way to do that using the gsubfn package

Gonçalo Ferraz wrote:
> Hi,
>
> I have a vector of strings (class character) with 6 elements (length  
> 6). I call it 'names'.
>
> "Graham Chapman"
> "John Cleese"
> "Terry Gilliam"
> "Eric Idle"
> "Terry Jones"
> "Michael Palin"
>
> And I want to turn it into another vector of strings called  
> 'shortnames' with the same length.
> The new vector should look like:
>
> "Gra Cha"
> "Joh Cle"
> "Ter Gil"
> "Eri Idl"
> "Ter Jon"
> "Mic Pal"
>
> I just want to clip the first three characters of each word inside  
> each string.
>
> Would anyone know how to do this quickly?
>
> Thanks!
>
> Gonçalo
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
>   
> ------------------------------------------------------------------------
>
> ______________________________________________
> 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.
>   


-- 
Mango Solutions
data analysis that delivers

Tel:  +44(0) 1249 467 467
Fax:  +44(0) 1249 467 468
Mob:  +44(0) 7813 526 123



More information about the R-help mailing list