Re: [R] spliting strings ... THANKS!

From: Monica Pisica <pisicandru_at_hotmail.com>
Date: Thu, 13 Dec 2007 14:24:25 +0000

Folks,  

Thanks a lot for a very speady answer. I usually read the list quite a bit because sometimes others put questions i may be interested in. One obs. i made was that lots of neat tricks can be made with apply, sapply and tapply. But even if i read all the help on these functions , and they seem obvious, i recognize that i don't understand them fully, and consequently i avoid them. Is there any detailed doc. that explains these functions?  

Monica> Date: Thu, 13 Dec 2007 15:06:27 +0100> From: P.Dalgaard_at_biostat.ku.dk> To: pisicandru_at_hotmail.com> CC: r-help_at_stat.math.ethz.ch> Subject: Re: [R] spliting strings ...> > 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?> > > > sapply(strsplit(str, " "), "[", 1)> [1] "aaa" "cc" "d" "mmm"> > -- > O__ ---- Peter Dalgaard ุster Farimagsgade 5, Entr.B> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918> ~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk) FAX: (+45) 35327907> >



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. Received on Thu 13 Dec 2007 - 14:29:04 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.