[R] spliting strings ...

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Dec 13 15:04:12 CET 2007


On Thu, 13 Dec 2007, Monica Pisica wrote:

>
> Hi everyone,
>
> I have a vector of strings, each string made up by different number of 
> words.

You need to define 'word' and 'first'.  Your solution says the first word 
of " aa" is "", which is not what most people would think.

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

or
> sub("([^ ]+).*", "\\1", str)

I don't see how you got your answer: R does not print like that (and never 
has).

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

PLEASE do!

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list