Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can two strings be concatenated?

How can I concatenate (merge, combine) two values? For example I have:

tmp = cbind("GAD", "AB") tmp #      [,1]  [,2] # [1,] "GAD" "AB" 

My goal is to concatenate the two values in "tmp" to one string:

tmp_new = "GAD,AB" 

Which function can do this for me?

like image 885
Hans Avatar asked Aug 26 '11 07:08

Hans


People also ask

How do you concatenate two strings in Python?

Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.

What is concatenation strings give example?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.

What is meant by concatenated string?

What Does Concatenation Mean? Concatenation, in the context of programming, is the operation of joining two strings together. The term"concatenation" literally means to merge two things together. Also known as string concatenation.

What is used for concatenation of strings?

The ampersand symbol is the recommended concatenation operator. It is used to bind a number of string variables together, creating one string from two or more individual strings.


1 Answers

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" 
like image 151
Rainer Avatar answered Oct 21 '22 14:10

Rainer