logo

How to Concatenate a Vector of Strings into One String in R 📂R

How to Concatenate a Vector of Strings into One String in R

Overview

R is a very convenient language for handling data, but if you are already familiar with other programming languages, the way R handles strings might feel somewhat unfamiliar. Unlike C or Python, R has a lot of built-in functionalities, and conversely, you often need to use these functionalities to handle data comfortably. It can be frustrating when built-in functions do not work as expected.

Example

20190605_115748.png For instance, let’s say we have a vector made up of characters as shown above. One might think to use the paste() function, which concatenates strings, but the result looks like this.

20190605_115758.png It turns into a weird state that is neither a vector nor a string. In this case, one should use the collapse option of the paste() function. The string that goes into the collapse option gets inserted between each vector and returns one string. In practice, if you insert a single space like collapse = " " and run it, you get the following result.

20190605_115811.png

Code

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