RE: [R] Combining the components of a character vector

About this list Date view Thread view Subject view Author view Attachment view

From: Adaikalavan Ramasamy (gisar@nus.edu.sg)
Date: Fri 04 Apr 2003 - 13:58:03 EST


Message-id: <024D6AEFCB92CB47BA1085751D184BB801053FA0@MBXSRV03.stf.nus.edu.sg>

Yes there is.

> x <- c("Bob", "loves", "Sally")
> paste(x, collapse=" ")
[1] "Bob loves Sally"

-----Original Message-----
From: John Miyamoto [mailto:jmiyamot@u.washington.edu]
Sent: Thursday, April 03, 2003 7:54 AM
To: R discussion group
Subject: [R] Combining the components of a character vector

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@u.washington.edu
Homepage http://faculty.washington.edu/jmiyamot/
--------------------------------------------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:40 EST