[R] spliting strings ...

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Dec 13 14:57:16 CET 2007


one way is the following:

str <- c('aaa bbb', 'cc', 'd eee aa', 'mmm o n')
sapply(strsplit(str, " "), "[", 1)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Monica Pisica" <pisicandru at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, December 13, 2007 2:41 PM
Subject: [R] spliting strings ...


>
> 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list