In this article, you’ll learn how to merge objects in R. Merging objects is a very frequently used concept in data analysis. R provides a broad range of capabilities to deal with merging data which are quite fundamental to R programming.
Merge two data frames by common columns or row names.
Usage
> merge(x, y, by, by.x, by.y, sort = TRUE)
Arguments
Value | Description |
x, y | data frames or objects to be coerced to one. |
by, by.x, by.y | specifications of the common columns. See Details. |
sort | logical. Should the results be sorted on the by columns? |
By default, the data frames are merged on the columns with names they both have, but separate specifcations of the columns can be given by by.x
and by.y
. Columns can be specified by name, number or by a logical vector: the name "row.names"
or the number 0
specifies the row names. The rows in the two data frames that match on the specified columns are extracted, and joined together. If there is more than one match, all possible matches contribute one row each.
If the remaining columns in the data frames have any common names, these have ".x"
and ".y"
appended to make the names of the result unique.
Adding Columns
To merge two data frames (datasets) horizontally, use the merge function. In most cases, you join two data frames by one or more common key variables (i.e., an inner join).
> df1 <- data.frame(x = month.abb, y = 1:12, z = letters[1:12])
> df1
x y z
1 Jan 1 a
2 Feb 2 b
3 Mar 3 c
4 Apr 4 d
5 May 5 e
6 Jun 6 f
7 Jul 7 g
8 Aug 8 h
9 Sep 9 i
10 Oct 10 j
11 Nov 11 k
12 Dec 12 l
> df2 <- data.frame(x = month.abb, a = 101:112, b = LETTERS[1:12])
> df2
x a b
1 Jan 101 A
2 Feb 102 B
3 Mar 103 C
4 Apr 104 D
5 May 105 E
6 Jun 106 F
7 Jul 107 G
8 Aug 108 H
9 Sep 109 I
10 Oct 110 J
11 Nov 111 K
12 Dec 112 L
Now, merging df1 and df2 data frames by column name ‘x’.
> DF <- merge(df1,df2,by='x')
> DF
x y z a b
1 Apr 4 d 104 D
2 Aug 8 h 108 H
3 Dec 12 l 112 L
4 Feb 2 b 102 B
5 Jan 1 a 101 A
6 Jul 7 g 107 G
7 Jun 6 f 106 F
8 Mar 3 c 103 C
9 May 5 e 105 E
10 Nov 11 k 111 K
11 Oct 10 j 110 J
12 Sep 9 i 109 I
Combine two Objects by Columns or Rows
Combine two “matrix-like” R objects by columns (cbind
) or rows (rbind
).
Usage
> cbind(x, y)
> rbind(x, y)
Arguments
Values | Description |
x | any R object, typically “matrix-like”. |
y | any R object, typically similar to x , or missing completely. |
Adding datasets vertically
To join two data frames (datasets) vertically, use the rbind()
function. The two data frames must have the same variables, but they do not have to be in the same order.
> df1 <- data.frame(x=letters[1:10], y=c(1:10))
> df1
x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
7 g 7
8 h 8
9 i 9
10 j 10
> df2 <- data.frame(x=letters[11:20], y=c(11:20))
> df2
x y
1 k 11
2 l 12
3 m 13
4 n 14
5 o 15
6 p 16
7 q 17
8 r 18
9 s 19
10 t 20
Now, applying rbind()
function to add the rows of df1 and df2 data frame.
> DF <- rbind(df1, df2)
> DF
x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
7 g 7
8 h 8
9 i 9
10 j 10
11 k 11
12 l 12
13 m 13
14 n 14
15 o 15
16 p 16
17 q 17
18 r 18
19 s 19
20 t 20
Adding datasets horizontally
To join two data frames (datasets) horizontally, use the cbind()
function. The two data frames must have the same number of rows, but they do not have to be in the same order nor save type.
# First data frame
> model <- data.frame(Make=c("Audi", "Ford", "BMW"), "No.of Model"=c(17, 12, 20))
> model
Make No.of.Model
1 Audi 17
2 Ford 12
3 BMW 20
# Second data frame
> sales <- data.frame(Make=c("Audi", "Ford", "BMW"), Sales=c(32123, 24124, 12342))
> sales
Make Sales
1 Audi 32123
2 Ford 24124
3 BMW 12342
Now, let’s add columns from both the data frames using cbind()
function
> cbind(model, sales)
make No.of.Model Make Sales
1 Audi 17 Audi 32123
2 Ford 12 Ford 24124
3 BMW 20 BMW 12342
Conclusion
Hence, we saw how to merge to object in R, and also saw the rbind()
and cbind()
function and how to use them.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!
Zigya Academy
BEING RELEVANT