Re: [R] spliting strings ...

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Thu, 13 Dec 2007 13:52:32 +0000

Hi,

if you only want the first word, then this should do the trick :

R> sub( " +.*", "", str )
[1] "aaa" "cc" "d" "mmm"

Cheers,

Romain

Monica Pisica wrote:
> Hi everyone,
>
> I have a vector of strings, each string made up by different number of words. I want to get a new vector which has only the first word of each string in the first vector. I came up with this:
>
> str <- c('aaa bbb', 'cc', 'd eee aa', 'mmm o n')
> str1 <- rep(1, length(str))
> for (i in 1:length(str)) {
> str1[i] <- strsplit(str, " ")[[i]][1]
> }
> str1
> 'aaa' 'cc' 'd' 'mmm'
>
> Now, is there any way to do this simpler?
>
> Thanks,
>
> Monica
>
> _________________________________________________________________
> Get the power of Windows + Web with the new Windows Live.
>
> [[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

______________________________________________
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.
Received on Thu 13 Dec 2007 - 14:02:38 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 Thu 13 Dec 2007 - 14:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.