[R] paste: multiple collapse arguments?

Moshe Olshansky m_olshansky at yahoo.com
Tue Aug 26 03:12:37 CEST 2008


One possibility is:
y <- rep(" ",6)
y[6] <- ""
y[c(2,4)] <- "\n"
res <- paste(paste(x,y,sep=""),collapse="")


--- On Tue, 26/8/08, remko duursma <remkoduursma at hotmail.com> wrote:

> From: remko duursma <remkoduursma at hotmail.com>
> Subject: [R] paste: multiple collapse arguments?
> To: r-help at r-project.org
> Received: Tuesday, 26 August, 2008, 9:36 AM
> Dear R-helpers,
> 
> I have a numeric vector, like:
> 
> x <- c(1,2,3,4,5,6)
> 
> I make this into a string for output to a text file,
> separated by \n:
> 
> paste(x, collapse="\n")
> 
> Is there a way to alternate the collapse argument? So
> between the first two elements of x, I want to separate by
> " ", then by "\n", and so forth.
> The result should then look like:
> "1 2\n3 4\n5 6"
> 
> (This way I get 2 elements of x on each line using
> writeLines, instead of one or all).
> I could do this in some ugly loop, but surely there is a
> better way?
> 
> thanks,
> Remko
> 
> 
> 
> 
> 
> 
> _________________________________________________________________
> Get thousands of games on your PC, your mobile phone, and
> the web with Windows®.
> 
> 	[[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