[R] String split and concatenation

Greg Snow Greg.Snow at imail.org
Wed Sep 29 20:32:02 CEST 2010


> paste( '(', paste( "'", rep(letters[1:3],2), "'", sep="", collapse=','), ')', sep="" ) 
[1] "('a','b','c','a','b','c')"

If you need the space after the comma then just change ',' to ', '.

The outer paste can be replaced with sprintf (and that may be more readable).

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Steven Kang
> Sent: Wednesday, September 29, 2010 2:16 AM
> To: Bill.Venables at csiro.au
> Cc: r-help at r-project.org
> Subject: Re: [R] String split and concatenation
> 
> x <- rep(letters[1:3], 2)
> 
> Are there any ways to transform & assign the above as the one shown
> below
> to an object? (in exact format; i.e length of 1 & class of character),
> i.e
> >x
> "('a', 'b', 'c', 'a', 'b', 'c')"
> 
> Highly appreciate for any advice.
> 
> 
> 
> On Wed, Sep 29, 2010 at 3:33 PM, <Bill.Venables at csiro.au> wrote:
> 
> > dump("x", file = "x.R")
> > file.show("x.R")
> >
> > will get you most of the way.
> >
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org]
> > On Behalf Of Steven Kang
> > Sent: Wednesday, 29 September 2010 3:11 PM
> > To: r-help at r-project.org
> > Subject: [R] String split and concatenation
> >
> > Hi R users,
> >
> >
> > I desire to transform the following vector consisting of repeated
> > characters
> >
> > x <- rep(letters, 3)
> > into this exact format (i.e a single string containing each
> characters in
> > quotation mark separated by comma between each; al ).
> >
> > ("a", "b", "c", "d", ........"a", "b", "c", "d", ..........., "a",
> "b",
> > "c",
> > "d", ........."z")
> >
> > Any advice would be much appreciated.
> >
> >
> >
> > --
> > Steven
> >
> >         [[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<http://www.r-
> project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 
> 
> --
> Steven
> 
> 	[[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.



More information about the R-help mailing list