ds1 = dataset into which you are moving information.
ds2= datset from which you are pulling new columns
KeyIdent = key identifier (e.g., subject number)
XYXY = dataset into which information is merged
*Preparing column names
*Combine all columns from separate datasets
*Combine select columns from one dataset
*Combine Rows from separate datasets
Preparing column names
You may want to verify that columns have intended names. This is especially important for the columns that will serve as the key identifier. If they do not match, R cannot integrate.
names(ds1) <- c("Subject", "Variable X1", "Variable X2")
names(ds2) <- c("Subject", "Variable Y1", "Variable Y2", "Variable Y3", "Variable Y4")
Combine all columns from separate datasets
In this example you are combining all columns from two datasets. "KeyIdent" is the identifier on which you are performing the merge...e.g., subject number.
XYXY <- merge(ds1, ds2, by = "KeyIdent", all.x = TRUE)
"all.x = TRUE" will keep all rows from ds1, even if ds2 has no match
"all.x = FALSE" will only keep rows with matches in both files
use "all.y" to base these commands on the contents of ds2
Combine select columns from one dataset
To add only certain columns from ds2 to an integrated dataset, along with all columns from ds1. "KeyIdent" is the identifier on which you are performing the merge...e.g., subject number.
Note that for ds2 "1" refers to the key identifier (e.g., subject number). "2, 4" refers to the columns that will be integrated.
Note that the same logic can be applied to ds1.
XYXY <- merge(ds1, ds2[c(1, 2, 4)], by = "KeyIdent", all.x = TRUE)
Combine Rows from separate datasets
Verify that both data files have the same columns
XYXY <-rbind(ds1, ds2)