How to merge R DataFrames of different length ?

06 Aug, 2021
In this article, we will discuss how to combine two dataframes of different length and make one final dataframe in the R Programming language. 
Create first dataframe
Create second dataframe
Use any function from the given below and combine them
Display dataset so created
R has an inbuilt function called merge which combines two dataframe of different lengths automatically.
Syntax:
merge(dataframe1, dataframe 2)
Example:
R
# 1st Dataframeemp.data <- data.frame(     emp_id = c (1:5),     emp_name = c("Ryan","Sita","Ram","Raj","Gargi"),     salary = c(62.3,151,311.0,429.0,822.25) ) # 2nd Dataframedf2 <- data.frame(     height = c ("5'3","5'11","4'9","6"),     weight = c(55.8,68,89,42.9)       ) merge(emp.data, df2)
  
Output:
emp_id emp_name salary height weight
1       1     Ryan  62.30    5’3   55.8
2       2     Sita 151.00    5’3   55.8
3       3      Ram 311.00    5’3   55.8
4       4      Raj 429.00    5’3   55.8
5       5    Gargi 822.25    5’3   55.8
6       1     Ryan  62.30   5’11   68.0
7       2     Sita 151.00   5’11   68.0
8       3      Ram 311.00   5’11   68.0
9       4      Raj 429.00   5’11   68.0
10      5    Gargi 822.25   5’11   68.0
11      1     Ryan  62.30    4’9   89.0
12      2     Sita 151.00    4’9   89.0
13      3      Ram 311.00    4’9   89.0
14      4      Raj 429.00    4’9   89.0
15      5    Gargi 822.25    4’9   89.0
16      1     Ryan  62.30      6   42.9
17      2     Sita 151.00      6   42.9
18      3      Ram 311.00      6   42.9
19      4      Raj 429.00      6   42.9
20      5    Gargi 822.25      6   42.9
 In this we will create a column named id for both the dataframes and combine the dataframes by the column id.
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:
R
# 1st Dataframeemp.data <- data.frame(     emp_id = c (1:5),     emp_name = c("Ryan","Sita","Ram","Raj","Gargi"),     salary = c(62.3,151,311.0,429.0,822.25) ) # 2nd Dataframedf2 <- data.frame(     height = c ("5'3","5'11","4'9","6"),     weight = c(55.8,68,89,42.9)       ) # creating the column id for dataframe 1emp.data = cbind("id"=rownames(emp.data),emp.data) # creating column id for dataframe 2df2 = cbind("id"=rownames(df2),df2) merge(emp.data,df2,all=T)
  
Output :
id emp_id emp_name salary height weight
1  1         Ryan  62.30    5’3   55.8
2  2         Sita  151.00   5’11   68.0
3  3         Ram   311.00    4’9   89.0
4  4         Raj   429.00      6   42.9
5  5         Gargi 822.25   <NA>     NA
 
adnanirshad158
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?