Concatenation in R

In R, concatenation is the process of combining multiple strings or vectors into a single string or vector. There are several ways to do concatenation in R, depending on the data structures. The first method is by using the paste() function.

The following data set contains first and last names of the players. First, create the vectors of first and last names by using the commands below

Download Example File
f_name <- c("Stephen", "Chris", "Derrick")
l_name <-  c("Cury", "Paul", "Rose")

Once the vectors have been created, the next step is to combine them. To combine these vectors, use the paste() function in the following way.

full1 <- paste(f_name,l_name)

The vectors created have been combined, showing first and last names of players together.

In the above image, the space has been added between first and last names by default. However, if you want to change the default setting of paste() function, by adding the comma instead of space separator in the two elements being combined, the following command should be used

full3 <- paste(f_name,l_name, sep = ",")

This will combine the elements by adding comma separator between them, as shown below

There is one more variation of paste() and that is paste0(). Because paste0() doesn’t have any default separator in it, like paste() function, it will combine vectors without putting any space between them. This is shown in the command as below.

full2 <- paste0(f_name,l_name)

The function will provide the following output

R concatenation

Similarly, if you want to combine strings and vectors together, the paste() function comes in handy too. For instance, if we want to create a sentence then we can use the following command.

full4 <- paste("The age of",f_name,l_name,"is",age)

The above command will combine the age of players with their respective names, as shown below

R concatenation and its usage

There is another way to combine these vectors using the paste() function through the collapse parameter. This parameter specifies how the concatenated elements will be joined together. Let’s use the following command

full5 <- paste(f_name,l_name,sep = ' ', collpase = 'and')

In this case, the string 'and' will be used to join all the concatenated elements. This means that after combining each first name and last name with a space separator, the entire resulting list of full names will be joined with the string “and”. The output of above command is shown as below

doing concatenation in R

Till now, we have worked with vectors for concatenation in R, next we move by using the data frame. The data frame is created by using the following command, which essentially produces the same result as of vectors.

Players <- data.frame(f_name = c("Stephen", "Chris" , "Derrick"),
                      l_name  = c("Cury", "Paul" , "Rose"),
                      age        = c("32" , "29" , "34"))

The data frame is as below

paste function in R

Next we run the tidyverse library, because in upcoming commands, we are going to use functions from the library. The tidyverse is run by following command

library(tidyverse)

Next, we want to create a new column, having full names of the players. This column is created by using the first and last names of the players. The mutate() function is used in the command to add a new column to the data frame. The command will be as below.

Players <- Players %>% mutate(full_name = paste(f_name,l_name))

After running this command, the Player’s data frame will have a new column “full_name” that contains the full names of each player, created by combining the f_name and l_name columns.

Concatenating data in R using Unite function

The data can also be concatenated using the unite function from the tidyverse package.

So the unite() function merges the specified columns together. So if we want to create a new column of full names of player by combining first and last names of players, using unite() function, use the following command

full_unite <- Players %>% unite(full_name,f_name,l_name)

The output is shown below

unite function in R

Notice that in above output, the separator of full name, by default, is underscore (_) rather than space. If you want to change the underscore to space, we use the following command

full_unite <- Players %>% unite(full_name,
                                                    f_name,l_name,
                                                    sep = " ")

This would add the space as a separator in full name, instead of underscore.

Similarly, in the above output, the first and last name columns are not included. To add these columns into output too, we can use the following command

full_unite <- Players %>% unite(full_name,
                                f_name,l_name,
                                sep = " " ,
                                remove = FALSE)

The “remove” parameter in the above command sets the FALSE function off, which would mean that now first and last names will be included in the output.

Combining Strings in R for concatenation

There is another function that works like paste function. This function is str_c() which combines multiple vectors or strings into a single vector or string.

Let’s use the following command to know how the str_c function works.

combine2 <- str_c(f_name,l_name, sep = " " , collapse = " and ")

This is almost the same command used for the paste() function, using the collapse parameter. However, the difference between output of paste() and str_c here is that former produces different string than the latter. The output for str_c is shown below

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin