Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to merge data frames by row names in R?
Mostly, we merge the data frames by columns because column names are considered prominent in data sets but it is also possible to merge two data frames by using rows. Merging by rows is likely to result in more uncleaned data as compared to the merging by columns. This can be done with the help of merge function and its by argument.
Example
Consider the below data frames −
df1Output
x1 x2 1 -0.47030794 0 2 0.86338465 8 3 -2.05770293 5 4 1.95479596 9 5 -0.06913421 5 6 0.64897263 5 7 -1.79859382 8 8 0.31247699 6 9 -0.36808285 7 10 -0.79578938 3Output
df2Output
x1 1 -0.01317184 2 0.01687606 3 -0.71685289 4 1.75961121 5 2.49024285 6 2.92183374 7 0.10276216 8 1.39703966 9 1.41001339 10 0.98221783Example
df3Output
x1 x2 x3 1 -0.4926244 2.697937 3.961118 2 1.9863263 2.861944 3.659564 3 -0.2266537 2.383499 3.208741 4 0.5966503 2.511485 3.230795 5 0.7148641 2.362419 4.582841Merging df1 with df2, df1 with df3, and df2 with df3 −
Example
df_1_2Output
Row.names x1.x x2 x1.y 1 1 -0.47030794 0 -0.01317184 2 10 -0.79578938 3 0.98221783 3 2 0.86338465 8 0.01687606 4 3 -2.05770293 5 -0.71685289 5 4 1.95479596 9 1.75961121 6 5 -0.06913421 5 2.49024285 7 6 0.64897263 5 2.92183374 8 7 -1.79859382 8 0.10276216 9 8 0.31247699 6 1.39703966 10 9 -0.36808285 7 1.41001339Example
df_1_3Output
Row.names x1.x x2.x x1.y x2.y x3 1 1 -0.47030794 0 -0.4926244 2.697937 3.961118 2 10 -0.79578938 3 NA NA NA 3 2 0.86338465 8 1.9863263 2.861944 3.659564 4 3 -2.05770293 5 -0.2266537 2.383499 3.208741 5 4 1.95479596 9 0.5966503 2.511485 3.230795 6 5 -0.06913421 5 0.7148641 2.362419 4.582841 7 6 0.64897263 5 NA NA NA 8 7 -1.79859382 8 NA NA NA 9 8 0.31247699 6 NA NA NA 10 9 -0.36808285 7 NA NA NAExample
df_2_3Output
Row.names x1.x x1.y x2 x3 1 1 -0.01317184 -0.4926244 2.697937 3.961118 2 10 0.98221783 NA NA NA 3 2 0.01687606 1.9863263 2.861944 3.659564 4 3 -0.71685289 -0.2266537 2.383499 3.208741 5 4 1.75961121 0.5966503 2.511485 3.230795 6 5 2.49024285 0.7148641 2.362419 4.582841 7 6 2.92183374 NA NA NA 8 7 0.10276216 NA NA NA 9 8 1.39703966 NA NA NA 10 9 1.41001339 NA NA NA
Advertisements
