The “merge” Function in R
Package: Base R (no specific package required)
Purpose: Merges two data frames based on common columns or row names.
General Class: Data Manipulation
Required Argument(s):
x, y: Data frames or objects that can be coerced to data frames.
...: Additional arguments specifying the columns or row names on which to merge.
Notable Optional Arguments:
by: A character vector specifying the common columns or row names by which to merge.
by.x, by.y: Character vectors specifying the columns or row names in x and y by which to merge.
all.x, all.y: Logical indicating whether to include all observations from x or y (or both) in the result.
suffixes: A character vector specifying the suffixes to be appended to the names of overlapping variables.
sort: Logical indicating whether to sort the result by the merged columns.
Example:
# Example data for merging data frames
df1 <- data.frame(ID = c(1, 2, 3), Name = c("Alice", "Bob", "Charlie"))
df2 <- data.frame(ID = c(2, 3, 4), Age = c(25, 30, 22))
# Merge data frames by the 'ID' column
merged_df <- merge(df1, df2, by = "ID")
# Display the merged data frame
print(merged_df)In this example, the merge function is used to merge two data frames (df1 and df2) based on the common column “ID.” The result is a merged data frame (merged_df) containing columns from both input data frames matched on the specified column.