How can I concatenate (merge, combine) two values? For example I have:
tmp = cbind(\"GAD\", \"AB\")
tmp
# [,1] [,2]
# [1,] \"GAD\" \"AB\"
paste()
is the way to go. As the previous posters pointed out, paste can do two things:
concatenate values into one "string", e.g.
> paste("Hello", "world", sep=" ")
[1] "Hello world"
where the argument sep
specifies the character(s) to be used between the arguments to concatenate,
or collapse character vectors
> x <- c("Hello", "World")
> x
[1] "Hello" "World"
> paste(x, collapse="--")
[1] "Hello--World"
where the argument collapse
specifies the character(s) to be used between the elements of the vector to be collapsed.
You can even combine both:
> paste(x, "and some more", sep="|-|", collapse="--")
[1] "Hello|-|and some more--World|-|and some more"
Hope this helps.
Given the matrix, tmp, that you created:
paste(tmp[1,], collapse = ",")
I assume there is some reason why you're creating a matrix using cbind, as opposed to simply:
tmp <- "GAD,AB"
glue
is a new function, data class, and package that has been developed as part of the tidyverse
, with a lot of extended functionality. It combines features from paste, sprintf, and the previous other answers.
tmp <- tibble::tibble(firststring = "GAD", secondstring = "AB")
(tmp_new <- glue::glue_data(tmp, "{firststring},{secondstring}"))
#> GAD,AB
Created on 2019-03-06 by the reprex package (v0.2.1)
Yes, it's overkill for the simple example in this question, but powerful for many situations. (see https://glue.tidyverse.org/)
Quick example compared to paste
with with
below. The glue
code was a bit easier to type and looks a bit easier to read.
tmp <- tibble::tibble(firststring = c("GAD", "GAD2", "GAD3"), secondstring = c("AB1", "AB2", "AB3"))
(tmp_new <- glue::glue_data(tmp, "{firststring} and {secondstring} went to the park for a walk. {firststring} forgot his keys."))
#> GAD and AB1 went to the park for a walk. GAD forgot his keys.
#> GAD2 and AB2 went to the park for a walk. GAD2 forgot his keys.
#> GAD3 and AB3 went to the park for a walk. GAD3 forgot his keys.
(with(tmp, paste(firststring, "and", secondstring, "went to the park for a walk.", firststring, "forgot his keys.")))
#> [1] "GAD and AB1 went to the park for a walk. GAD forgot his keys."
#> [2] "GAD2 and AB2 went to the park for a walk. GAD2 forgot his keys."
#> [3] "GAD3 and AB3 went to the park for a walk. GAD3 forgot his keys."
Created on 2019-03-06 by the reprex package (v0.2.1)
You can create you own operator :
'%&%' <- function(x, y)paste0(x,y)
"new" %&% "operator"
[1] newoperator`
You can also redefine 'and' (&
) operator :
'&' <- function(x, y)paste0(x,y)
"dirty" & "trick"
"dirtytrick"
messing with baseline syntax is ugly, but so is using paste()/paste0()
if you work only with your own code you can (almost always) replace logical & and
operator with *
and do multiplication of logical values instead of using logical 'and &'
> tmp = paste("GAD", "AB", sep = ",")
> tmp
[1] "GAD,AB"
I found this from Google by searching for R concatenate strings: http://stat.ethz.ch/R-manual/R-patched/library/base/html/paste.html
For the first non-paste()
answer, we can look at stringr::str_c()
(and then toString()
below). It hasn't been around as long as this question, so I think it's useful to mention that it also exists.
Very simple to use, as you can see.
tmp <- cbind("GAD", "AB")
library(stringr)
str_c(tmp, collapse = ",")
# [1] "GAD,AB"
From its documentation file description, it fits this problem nicely.
To understand how str_c works, you need to imagine that you are building up a matrix of strings. Each input argument forms a column, and is expanded to the length of the longest argument, using the usual recyling rules. The sep string is inserted between each column. If collapse is NULL each row is collapsed into a single string. If non-NULL that string is inserted at the end of each row, and the entire matrix collapsed to a single string.
Added 4/13/2016: It's not exactly the same as your desired output (extra space), but no one has mentioned it either. toString()
is basically a version of paste()
with collapse = ", "
hard-coded, so you can do
toString(tmp)
# [1] "GAD, AB"