Skip to content

Combining the components of a character vector

14 messages · John Miyamoto, Peter Dalgaard, strumila network systems +11 more

#
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/
--------------------------------------------------------------------
#
John Miyamoto <jmiyamot at u.washington.edu> writes:
Like this:
[1] "Bob loves Sally"
#
paste(x,collapse=" ")

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of John Miyamoto
Sent: Thursday, 3 April 2003 9: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 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
#
On Thursday 03 April 2003 01:54, John Miyamoto wrote:

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

For further info a look at help(paste) might help
Z
#
R> x <- c("Bob", "loves", "Sally")
R> paste(x, collapse=" ")
[1] "Bob loves Sally"

best,

Torsten
#
On Thursday 03 Apr 2003 1:54 am, John Miyamoto wrote:
y <- paste(c, collapse=" ")

best wishes

Ido
#
[1] "Bob loves Sally"

best,
vito


----- Original Message ----- 
From: "John Miyamoto" <jmiyamot at u.washington.edu>
To: "R discussion group" <r-help at stat.math.ethz.ch>
Sent: Thursday, April 03, 2003 1:54 AM
Subject: [R] Combining the components of a character vector
#
John,

Try paste with collapse argument:
[1] "Bob loves Sally"

HTH
steve
John Miyamoto wrote:

            
#
paste(x,collapse=" ")
On Wed, 2 Apr 2003, John Miyamoto wrote:

            

  
    
#
Use the collapse argument to paste.
[1] "Bob loves Sally"

John Miyamoto <jmiyamot at u.washington.edu> writes:
#
paste( c("Bob", "loves", "Sally"), collapse=" ")

Spencer Graves
John Miyamoto wrote:
#
Please, have a closer look at the help file for paste(), and use the 
"collapse" arguments.

Jerome
On April 2, 2003 03:54 pm, John Miyamoto wrote:
#
Try the "collapse" argument in paste(), i.e.
    paste(x,collapse=" ")
John Miyamoto wrote:

  
    
#
<snippage>
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