logo

How to Combine a Vector of Strings into a Single String in R 📂R

How to Combine a Vector of Strings into a Single String in R

Overview

R is an extremely convenient language for handling data, but for those who are also familiar with other programming languages, R’s strings may feel somewhat unfamiliar. Unlike C or Python, R itself supports many features, and conversely, you can only handle things smoothly if you use those features. So when the built-in functions do not work the way you expect, it can be frustrating.

Example

20190605\_115748.png For instance, suppose we have a vector made up of characters as shown above. The easy thought is that we could just use paste(), the function that combines strings, but the result is as follows.

20190605\_115758.png It ends up in a strange state that is neither a vector nor a string. In such cases, you can use the collapse option of the paste() function. The string passed to the collapse option is inserted between each element of the vector, and a single string is returned. Indeed, if you run it with a single space, such as collapse = " ", you obtain the following result.

20190605\_115811.png

Code

Carte<-c("나는","생각한다.","고로","존재한다."); Carte
 
paste(Carte)
 
paste(Carte,collapse = " ")