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/ --------------------------------------------------------------------
Peter Dalgaard BSA
2003-Apr-03 12:19 UTC
[R] Combining the components of a character vector
John Miyamoto <jmiyamot at u.washington.edu> writes:> 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"Like this:> paste(x,collapse=" ")[1] "Bob loves Sally" -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
strumila network systems
2003-Apr-03 12:20 UTC
[R] Combining the components of a character vector
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:> 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"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> 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
> 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" >R> x <- c("Bob", "loves", "Sally") R> paste(x, collapse=" ") [1] "Bob loves Sally" best, Torsten> 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 Apr 2003 1:54 am, 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" .y <- paste(c, collapse=" ") best wishes Ido
> x <- c("Bob", "loves", "Sally") > paste(x,collapse=" ")[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> 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
John, Try paste with collapse argument:> x <- c("Bob", "loves", "Sally") > paste(x,collapse=" ")[1] "Bob loves Sally" HTH steve 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
John Miyamoto wrote:> 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"> ....Try:> paste(x,collapse=" ")and> help(paste)--Peter ------------------------------------------------------------------------------- Peter Wolf, Statistik/Informatik, Fak.f.Wiwi, Uni Bielefeld pwolf@wiwi.uni-bielefeld.de, http://www.wiwi.uni-bielefeld.de/~wolf/wolf.html ------------------------------------------------------------------------------- [[alternate HTML version deleted]]
The collapse argument does what you want: x <- c("Bob", "loves", "Sally") paste(c, collapse = " ") Hope this helps, Matt Wiener -----Original Message----- From: John Miyamoto [mailto:jmiyamot at u.washington.edu] Sent: Wednesday, April 02, 2003 6:54 PM 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 ------------------------------------------------------------------------------
paste(x,collapse=" ") On Wed, 2 Apr 2003, 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 >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704
Use the collapse argument to paste.> paste(c('Bob', 'loves', 'Sally'), collapse = ' ')[1] "Bob loves Sally" John Miyamoto <jmiyamot at u.washington.edu> writes:> Suppose I have a character vector. > > x <- c("Bob", "loves", "Sally") > > I want to combine it into a single string: "Bob loves Sally" .
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 -----------------------------------------------------
Jean-Pierre.Mueller@dssp.unil.ch
2003-Apr-03 15:30 UTC
[R] Combining the components of a character vector
At 15:54 -0800 2.4.2003, 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" . >[...] >Is there a more natural (no loop) way to do this in R? > >John Miyamotopaste(x, sep = "", collapse = " ") ? ------------------------------------------------------------------------------ Jean-Pierre Muller SSP / UNIL / BFSH2 / CH-1015 Lausanne
paste( c("Bob", "loves", "Sally"), collapse=" ") Spencer Graves 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
From ?paste:> If a value is specified for `collapse', the values in the result > are then concatenated into a single string, with the elements > being separated by the value of `collapse'.> paste(c("Bob", "loves", "Sally"), collapse=" ") [1] "Bob loves Sally" > At Wednesday 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 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
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:> 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
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." -----------------------------------------------------------------
> 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" > > Is there a more natural (no loop) way to do this in R? >RTFM:> paste(x, collapse=" ")[1] "Bob loves Sally" Ray
Adaikalavan Ramasamy
2003-Apr-04 03:58 UTC
[R] Combining the components of a character vector
Yes there is.> x <- c("Bob", "loves", "Sally") > paste(x, collapse=" ")[1] "Bob loves Sally" -----Original Message----- From: John Miyamoto [mailto:jmiyamot at 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 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