[R] Combining the components of a character vector

Pierre Kleiber pkleiber at honlab.nmfs.hawaii.edu
Thu Apr 3 20:51:39 CEST 2003


Try the "collapse" argument in paste(), i.e.
    paste(x,collapse=" ")


John Miyamoto wrote:
> Dear Help,
>    Suppose I have a character vector.
> 
> x <- c("Bob", "loves", "Sally")
> 
> I want to combine it into a single string:  "Bob loves Sally" .
> paste(x) yields:
> paste(x)
> [1] "Bob"   "loves" "Sally"
> 
> 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?
> 
> John Miyamoto
> 
> --------------------------------------------------------------------
> John Miyamoto, Dept. of Psychology, Box 351525
> University of Washington, Seattle, WA 98195-1525
> Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu
> Homepage http://faculty.washington.edu/jmiyamot/
> --------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 


-- 
-----------------------------------------------------------------
Pierre Kleiber             Email: pkleiber at honlab.nmfs.hawaii.edu
Fishery Biologist                     Tel: 808 983-5399/737-7544
NOAA FISHERIES - Honolulu Laboratory         Fax: 808 983-2902
2570 Dole St., Honolulu, HI 96822-2396
-----------------------------------------------------------------
  "God could have told Moses about galaxies and mitochondria and
   all.  But behold... It was good enough for government work."
-----------------------------------------------------------------



More information about the R-help mailing list