Introduction
Merging data frames is a common task in data analysis, where you combine data from multiple sources into a single data frame based on one or more common columns. In R, the merge() function is used to merge two data frames. This guide will walk you through writing an R program to merge two data frames.
Problem Statement
Create an R program that:
- Creates two data frames.
- Merges the data frames based on a common column.
- Displays the merged data frame.
Example:
- Input:
- Data Frame 1:
Name,Age - Data Frame 2:
Name,Score - Merge based on the
Namecolumn.
- Data Frame 1:
- Output: A merged data frame containing
Name,Age, andScore.
Solution Steps
- Create Two Data Frames: Use the
data.frame()function to create two data frames with a common column. - Merge the Data Frames: Use the
merge()function to merge the two data frames based on the common column. - Display the Merged Data Frame: Use the
print()function to display the merged data frame.
R Program
# R Program to Merge Two Data Frames
# Author: Ramesh Fadatare
# Step 1: Create the first data frame
df1 <- data.frame(Name = c("Ramesh", "Suresh", "Mahesh", "Ganesh"),
Age = c(25, 30, 22, 28))
# Step 2: Create the second data frame
df2 <- data.frame(Name = c("Ramesh", "Suresh", "Rajesh", "Ganesh"),
Score = c(85.5, 90.0, 88.5, 92.0))
# Step 3: Merge the two data frames based on the 'Name' column
merged_df <- merge(df1, df2, by = "Name")
# Step 4: Display the merged data frame
print("Merged Data Frame:")
print(merged_df)
Explanation
Step 1: Create the First Data Frame
- The first data frame
df1is created using thedata.frame()function, with columns forNameandAge.
Step 2: Create the Second Data Frame
- The second data frame
df2is created using thedata.frame()function, with columns forNameandScore.
Step 3: Merge the Two Data Frames Based on the Name Column
- The
merge()function is used to merge the two data frames,df1anddf2, based on the commonNamecolumn. Thebyparameter specifies the column used for merging.
Step 4: Display the Merged Data Frame
- The
print()function is used to display the merged data frame, which includes theName,Age, andScorecolumns.
Output Example
Example:
[1] "Merged Data Frame:"
Name Age Score
1 Ganesh 28 92.0
2 Ramesh 25 85.5
3 Suresh 30 90.0
Conclusion
This R program demonstrates how to merge two data frames using the merge() function. It covers basic operations such as data frame creation, merging based on a common column, and displaying the result. Merging data frames is a crucial operation in data analysis, allowing you to combine datasets from different sources effectively. This example is particularly useful for anyone learning how to manipulate and analyze data frames in R.