Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate a vector of strings/character

Tags:

string

r

r-faq

If I have a vector of type character, how can I concatenate the values into string? Here's how I would do it with paste():

sdata = c('a', 'b', 'c') paste(sdata[1], sdata[2], sdata[3], sep ='') 

yielding "abc".

But of course, that only works if I know the length of sdata ahead of time.

like image 729
Nick Avatar asked Jan 20 '10 01:01

Nick


People also ask

Which character 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.

Can we concatenate string and character?

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function. In the above example, we have declared two char arrays mainly str1 and str2 of size 100 characters.

How do I concatenate elements in a vector in R?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.


1 Answers

Try using an empty collapse argument within the paste function:

paste(sdata, collapse = '')

Thanks to http://twitter.com/onelinetips/status/7491806343

like image 192
Matt Turner Avatar answered Sep 17 '22 17:09

Matt Turner