[R] Combining the components of a character vector

Thomas Lumley tlumley at u.washington.edu
Fri Apr 4 16:51:02 CEST 2003


> On Thursday 03 April 2003 01:54, John Miyamoto wrote:

<snippage>

> > The following function combines the character vector into a string
> > in the way that I want, but it seems somewhat inelegant.
> >
> > paste.vector <- function(x, ...) {
> > 	output <- NULL
> > 	for (i in 1:length(x)) output <- paste(output, x[i], ...)
> > 	output	} #end of function definition
> >
> > paste.vector(x)
> > [1] " Bob loves Sally"
> >
> > Is there a more natural (no loop) way to do this in R?

I might also take this opportunity to note that if paste() didn't have the
collapse=  argument there would still be more elegant way to write the
loop

   do.call("paste",as.list(x))

creates a call to paste() whose arguments are the elements of x.


	-thomas



More information about the R-help mailing list