[R] splitting a character vector

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Mar 28 19:17:43 CEST 2004


It would be clearer to use

strsplit(xa, " ")[[1]]

the point being that you do want the result for the first element of the 
first argument, picked out by [[1]].

On Sun, 28 Mar 2004, John Fox wrote:

> Dear Erin,
> 
> unlist(strsplit(xa, " ")) should give you what you want; note that you don't
> need c() to define xa in your example.
> 
> I hope this helps,
>  John 
> 
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch 
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Erin Hodgess
> > Sent: Sunday, March 28, 2004 12:03 PM
> > To: r-help at stat.math.ethz.ch
> > Subject: [R] splitting a character vector
> > 
> > Dear R People:
> > 
> > Suppose I have the following;
> > 
> > xa <- c("There are 5 dogs")
> > 
> > I would like to have a new character vector such that xb[1] 
> > is There xb[2] is are xb[3] is 5 xb[4] is dogs
> > 
> > Since the original vector has length 1, substring will not work.
> > 
> > Any suggestions would be MOST welcome!
> > 
> > thanks
> > Erin Hodgess
> > Associate Professor
> > Department of Computer and Mathematical Sciences University 
> > of Houston - Downtown
> > mailto: hodgess at gator.uhd.edu
> > 
> > R 1.8.1 for Windows
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 

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