Here we are explaining how to write an R program to create inner, outer, left, right join(merge) from given two data frames. Here we are using a built-in function data.frame(),merge(). A data frame is used for storing data tables which has a list of vectors with equal length. The data frames are created by function data.frame(), which has tightly coupled collections of variables. And the function merge() helps to merge two data frames by common columns or row names, or database join operations. The syntax of this function is,
– where x, y are data frames, or objects to be coerced to one. And the dots(…) indicates arguments to be passed to or from methods.
Below are the steps used in the R program to create inner, outer, left, right join from the given two data frames. In this R program, we directly give the data frame to a built-in function. Here we are using variables D1, D2 for holding data frames. Call the function data.frame() for creating data frame. Finally, merge the given data frame by calling merge function differently.
D1 = data.frame(data = c(12, 14, 10, 11))
D2 = data.frame(data = c(13, 15, 11, 12))
print("Left outer Join:")
## [1] "Left outer Join:"
result = merge(D1, D2, by = "data", all.x = TRUE)
print(result)
## data
## 1 10
## 2 11
## 3 12
## 4 14
print("Right outer Join:")
## [1] "Right outer Join:"
result = merge(D1, D2, by = "data", all.y = TRUE)
print(result)
## data
## 1 11
## 2 12
## 3 13
## 4 15
print("Outer Join:")
## [1] "Outer Join:"
result = merge(D1, D2, by = "data", all = TRUE)
print(result)
## data
## 1 10
## 2 11
## 3 12
## 4 13
## 5 14
## 6 15
print("Inner Join:")
## [1] "Inner Join:"
result = merge(D1, D2, by = "data", all = FALSE)
print(result)
## data
## 1 11
## 2 12
print("Cross Join:")
## [1] "Cross Join:"
result = merge(D1, D2, by = NULL)
print(result)
## data.x data.y
## 1 12 13
## 2 14 13
## 3 10 13
## 4 11 13
## 5 12 15
## 6 14 15
## 7 10 15
## 8 11 15
## 9 12 11
## 10 14 11
## 11 10 11
## 12 11 11
## 13 12 12
## 14 14 12
## 15 10 12
## 16 11 12