Combining the components of a character vector
Dear John, Try paste(x, collapse=" ") John
At 03:54 PM 4/2/2003 -0800, 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 Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox -----------------------------------------------------