Learn R Programming

lessR (version 2.8)

Merge: Merge Two Data Frames Horizontally or Vertically

Description

Abbreviation: mrg

Based directly on the standard R merge function for a vertical merge and the rbind function for a horizontal merge.

Usage

Merge(data1, data2, by=NULL, quiet=FALSE, ...)

mrg(...)

Arguments

data1
The name of the first data frame from which to create the merged data frame.
data2
The name of the second data frame from which to create the merged data frame.
by
If specified, then signals a vertical merge and the variable by which the data frames are merged. Specify "row.names" for merging according to the row names.
quiet
If set to TRUE, no text output.
...
The specified arguments.

Value

  • The merged data frame is returned, usually assigned the name of mydata as in the examples below. This is the default name for the data frame input into the lessR data analysis functions.

Details

Merge creates a merged data frame from two input data frames.

If by is specified the merge is vertical. That is the variables in the second input data frame are presumed different from the variables in the first input data frame. The merged data frame is the combination of variables from both input data frames, with the rows aligned by the value of by, a variable common to both data frames. Merge relies upon the R function rbind for the vertical merge.

If by is not provided, then the merge is horizontal. The variables are presumed the same in each input data frame. The merged data frame consists of the rows of both input data frames. Merge relies upon the R function merge for the horizontal merge.

Guidance and feedback regarding the merge are provided by default. The first five lines of each of the input data frames are listed before the merge operation, followed by the first five lines of the output data frame.

See Also

merge, rbind.

Examples

Run this code
# construct the data frames to merge
mydata <- Read("Employee", format="lessR")
Emp1a <- mydata[1:4, c("Years", "Gender", "Dept", "Salary")]
Emp1b <- mydata[1:4, c("Satisfaction", "HealthPlan")]
Emp2a <- mydata[1:4, ]
Emp2b <- mydata[7:10, ]
rm(mydata)

# horizontal merge
mydata <- Merge(Emp1a, Emp1b, by="row.names")

# vertical merge
mydata <- Merge(Emp2a, Emp2b)

Run the code above in your browser using DataLab