Open In App

How to Combine Two Columns into One in R dataframe?

Last Updated : 04 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to combine two columns into one in dataframe in R Programming Language. 

Method 1 : Using paste() function

This function is used to join the two columns in the dataframe with a separator.

Syntax:

paste(data$column1, data$column2, sep=" ")

where

  • data is the input dataframe
  • column1 is the first column
  • column2 to is the second column
  • sep is the separator to be separated between two columns

Example 1:

R




# create dataframe
data = data.frame(firstname=c("akash", "kyathi", "preethi"),
                  lastname=c("deep", "lakshmi", "savithri"),
                  marks=c(89, 96, 89))
 
# display
print(data)
 
# combine first name and last name columns
# with blank separator
data$fullname = paste(data$firstname, data$lastname, sep=" ")
 
# display
data


Output:

Example 2:

R




# create dataframe
data = data.frame(firstname=c("akash", "kyathi", "preethi"),
                  lastname=c("deep", "lakshmi", "savithri"),
                  marks=c(89, 96, 89))
 
# display
print(data)
 
# combine first name and last name columns
# with blank separator
data$fullname = paste(data$firstname, data$lastname, sep="--")
 
# display
data


Output:

Method 2: Using unite() function

This function is available in tidyr package and we have to load that package and will combine columns.

Syntax:

unite(dataframe, combined_columnname, c(columns))

where,

  • dataframe is the input dataframe
  • columns are the dataframe columns to be combines
  • combined_columnname is the name of the combined column

Example:

R




# import tidyr package
library(tidyr)
 
# create dataframe
data = data.frame(firstname=c("akash", "kyathi", "preethi"),
                  lastname=c("deep", "lakshmi", "savithri"),
                  marks=c(89, 96, 89))
 
# display
print(data)
 
# combine first name and last name columns
# with blank separator
data = unite(data, fullname, c(firstname, lastname))
 
# display
data


Output:



Similar Reads

Combine Multiple Excel Worksheets into Single Dataframe in R
In this article, we will discuss how to combine multiple excel worksheets into a single dataframe in R Programming Language. The below XLSX file "gfg.xlsx" has been used for all the different approaches. Method 1: Using readxl package The inbuilt setwd() method is used to set the working directory in R. The readxl package in R is used to import and
4 min read
Combine two DataFrames in R with different columns
In this article, we will discuss how to combine two dataframes with different columns in R Programming Language. Method 1 : Using plyr package The "plyr" package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the following command : install.packages("plyr") rbin
5 min read
Combine two ggplot2 plots from different DataFrame in R
In this article, we are going to learn how to Combine two ggplot2 plots from different DataFrame in R Programming Language. Here in this article we are using a scatter plot, but it can be applied to any other plot. Let us first individually draw two ggplot2 Scatter Plots by different DataFrames then we will see how to combine them i.e how draw both
2 min read
Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns. Syntax: cbind(x1, x2, ..., deparse.level = 1) Parameters: x1, x2: vector, matrix, data frames deparse.level: This value determines how the column names generated. The default value of deparse.level is 1. Example 1: # R program to illustrate # cbind
2 min read
Split DataFrame Variable into Multiple Columns in R
In this article, we will discuss how to split dataframe variables into multiple columns using R programming language. Method 1: Using do.call method The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into subparts. Syntax: strsplit(str, pattern) Parameter
3 min read
How to Split Column Into Multiple Columns in R DataFrame?
In this article, we will discuss how to split a column from a data frame into multiple columns in the R programming Language. Method 1: Using str_split_fixed() function of stringr package library To split a column into multiple columns in the R Language, We use the str_split_fixed() function of the stringr package library. The str_split_fixed() fun
3 min read
Combine Arguments into a Vector in R Programming - c() Function
c() function in R Language is used to combine the arguments passed to it. Syntax: c(...) Parameters: ...: arguments to be combined Example 1: # R program to cumulative maxima # Calling c() function x <- c(1, 2, 3, 4) x Output: [1] 1 2 3 4 Example 2: # R program to cumulative maxima # Calling c() function x <- c("a", "b",
1 min read
Append one dataframe to the end of another dataframe in R
In this article, we are going to append the data frame to another data frame using "$" operator in R Programming Language. Approach Create vectorsCreate one dataframe (dataframe1) by passing these vectorsCreate another dataframe (dataframe2)by passing these vectorsFinally, append the dataframe2 to dataframe1 using" $" operator.Display resultant dat
2 min read
How to find common rows and columns between two dataframe in R?
Two data frames can have similar rows, and they can be determined. In this article, we will find the common rows and common columns between two data frames, in the R programming language. Approach Create a first data frameCreate a second data frameCompare using required functionsCopy same rows to another data frameDisplay data frame so generated. D
2 min read
Sum of Two or Multiple DataFrame Columns in R
In this article, we will discuss how to perform some of two and multiple dataframes columns in R programming language. Database in use: Sum of two columns The columns whose sum has to be calculated can be called through the $ operator and then we can perform the sum of two dataframe columns by using "+" operator. Syntax: dataframe$column1 + datafra
2 min read