Concatenate two columns of dataframe in R

To Concatenate two columns of dataframe in R we generally use paste() Function. Concatenate or join of two string column in R  & integer columns in R is accomplished by Paste() function. we can also concatenate or join numeric and string column.  Let’s see how to

  • Concatenate two columns of dataframe in R.
  • Concatenate numeric and string column in R.
  • Concatenate two columns by removing leading and trailing space.
  • Concatenate two or more columns using hyphen(“-”) & space
  • merge or concatenate two or more columns in R using str_c() and unite() function.

Let’s first create the dataframe.

### Create Data Frame

df1 = data.frame(State = c('Arizona','Georgia','Newyork','Indiana','Florida'), 
                 State_code = c('AZ','GG','NY','IN','FL'), 
                 Score=c(62,47,55,74,31))
df1

So the resultant dataframe will be

Concatenate two columns of dataframe in R 1

 

Concatenate two string columns:

Let’s concatenate two columns of dataframe with paste function as shown below. paste() function takes up two or more column as argument concatenates them to a single column with default space 

### Concatenate two string columns

df1$state_and_code = paste(df1$State,df1$State_code)
df1

So the result dataframe takes up “state” column and “state_code” column and concatenates them with space 

Concatenate two columns of dataframe in R 2

 

Concatenate two string columns using paste0() function:

Let’s concatenate two columns of dataframe with paste0() function as shown below. paste0() function takes up two or more column as argument concatenates them to a single column without any space 

### Concatenate two string columns

df1$state_and_code = paste0(df1$State,df1$State_code)
df1

So the result dataframe takes up “state” column and “state_code” column and concatenates them without space 

Concatenate two columns of dataframe in R 8

 

Concatenate two columns with space in R:

Let’s concatenate two columns of dataframe with + as shown below. paste() function takes up two or more column as argument along with “+” which  concatenates them to a single column with “+” as separator.

### Concatenate two string columns with "+"

df1$state_and_code = paste(df1$State,"+",df1$State_code)
df1

So the result will be

Concatenate two columns of dataframe in R 3

 

Concatenate two columns with – (hyphen) in R:

Let’s concatenate two columns of dataframe with – (hyphen) as shown below

### Concatenate two string columns with hypen

df1$state_and_code = paste(df1$State,"-",df1$State_code)
df1

So the result will be

Concatenate two columns of dataframe in R 4

 

Concatenate numeric and string columns with (+) in R:

Let’s concatenate a numeric column and string column of dataframe with + as shown below

### Concatenate numeric and string column

df1$state_code_and_score = paste(df1$State_code,"+",df1$Score) 
df1

So the result will be

Concatenate two columns of dataframe in R 6

 

Concatenate two columns using str_c() function in R:

Let’s concatenate two columns of dataframe using str_c() function. str_c() function is from “tidyverse” package. it takes up two column names and a separator as argument  as shown below

### Concatenate two string columns using str_c() function

library(tidyverse)<br />df1$state_and_code  = str_c(df1$State," - ",df1$State_code)<br />

So the result will be

Concatenate two columns of dataframe in R 4

 

 

Concatenate two columns using unite() function in R:

unite() function takes up the columns “State” and “State_code” then concatenates them using underscore(_) as default.

### concatenate two or more columns using unite() function

df1 %&gt;% unite("state_and_code", State:State_code, remove = FALSE)

so the result will be

Concatenate two columns of dataframe in R 9

 

 

Concatenate two columns and remove leading & trailing space in R:

Let’s concatenate two columns and remove leading & trailing space in R using paste() function and trimws() function. First paste() function concatenates two columns and then trimws() function removes  both leading and trailing spaces

## Concatenate two columns using space

df1$state_and_code = trimws(paste(df1$State,"-",df1$State_code), which = c("both"))
df1

So the result will be

Concatenate two columns of dataframe in R 7

 

 

                                                                                                           

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.