- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to merge two data frames of different length having same values in all columns but at different positions?
To merge two data frames of different length having same values in all columns but at different positions, we can follow the below steps −
- First of all, create two data frames.
- Then, merge them using merge function with all argument set to FALSE.
Create the data frame
Let's create a data frame as shown below −
> x<-sample(1:5,20,replace=TRUE) > y<-sample(1:5,20,replace=TRUE) > z<-sample(1:5,20,replace=TRUE) > df1<-data.frame(x,y,z) > df1
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Output
x y z 1 2 3 5 2 3 3 2 3 5 3 1 4 1 2 3 5 1 3 2 6 2 5 4 7 5 5 3 8 1 1 1 9 2 1 2 10 3 4 1 11 2 4 1 12 3 5 1 13 4 3 1 14 3 1 3 15 4 5 2 16 1 3 3 17 4 2 2 18 3 2 3 19 2 4 5 20 3 3 1
Let’s create a data frame df2 as shown below −
> x<-sample(1:5,20,replace=TRUE) > y<-sample(1:5,20,replace=TRUE) > z<-sample(1:5,20,replace=TRUE) > df2<-data.frame(x,y,z) > df2
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z 1 3 3 2 2 3 1 2 3 4 5 5 4 3 2 3 5 1 1 5 6 3 1 4 7 5 5 4 8 4 1 2 9 2 1 1 10 3 1 1 11 2 2 2 12 5 5 1 13 4 3 5 14 1 1 1 15 1 2 1 16 1 3 2 17 4 5 1 18 1 2 4 19 2 4 2 20 5 4 4
Merge the two data frames
Using merge function to merge df1 and df2 −
> DF<-merge(df1,df2,by=c("x","y","z"),all=FALSE) > DF
x y z 1 1 1 1 2 1 3 2 3 3 2 3 4 3 3 2
- Related Articles
- How to create the combination of rows in two data frames having same columns in R?
- Represent graphically by two separate diagrams in each case$(i)$. Two sound waves having the same amplitude but different frequencies.$(ii)$. Two sound waves having the same frequency but different amplitudes.$(iii)$. Two sound waves having different amplitudes and also different wave lengths.
- How to merge rows having same values in an R data frame?
- How to merge queries in a single MySQL query to get the count of different values in different columns?
- How to add values in columns having same name and merge them in R?
- How to merge two unequal data frames and replace missing values to 0 in R?
- How to select different values from same column and display them in different columns with MySQL?
- Python - Merge DataFrames of different length
- How to join two data frames based one factor column with different levels and the name of the columns in R using dplyr?
- How to merge two different array of objects using JavaScript?
- How to move data between two tables with columns in different MySQL databases?
- Atoms of elements having the same mass number but different atomic number are called ?
- If two bodies travel at the same speed but in different directions, they will have a. Different velocities b. Same velocities. C. Same Displacement d. None of these
- How to multiply corresponding values from two data frames in R?
- How to merge data frames by row names in R?

Advertisements