Introduction
If you have datasets of identical structure (in terms of number of columns and variable names) that need to be merged, we’ll show you how to use the rbind command in R to combine them.
Create Data
Let’s say we have two datasets (which R calls data frames). The first dataset contains information on subjects 1-10, the second on subjects 11 through 25. Each dataset has IQ and income information. We want to combine the datasets. Before we do so, let’s create the data frames in R.
First Dataset
Try the following code:
iq2 <- rnorm(10, mean=100, sd=15)
iq <- round(iq2)
income2 <- rnorm(10, mean=55000, sd=5000)
income <- round(income2)
subj <-1:10
group1data <- data.frame(subj, iq, income)
print(group1data)
Second Dataset
Try the following code:
iq2 <- rnorm(15, mean=115, sd=15)
iq <- round(iq2)
income2 <- rnorm(15, mean=85000, sd=9000)
income <- round(income2)
subj <- 11:25
group2data <- data.frame(subj, iq, income)
print(group2data)
Combine Datasets
Now try the following code:
merged_data <- rbind(group1data, group2data)
print(merged_data)
The rbind command works if you have the same number of columns in the datasets you’re attempting to bind. You also need to have the same variable names.
BridgeText can help you with all of your statistical analysis needs.