Wednesday, August 08, 2007

Merge two data sets in R

R can merge two data sets that have a same type of reference variable. Intuitively, the command is "merge."

# example
dat1 <- data.frame(id=c(1, 1, 1, 2, 2, 2),
sex=c("m", "m", "m", "f", "f", "f"))
dat2 <- data.frame(p.id=c(1, 2), income=c(23451, 21232))
merge(dat1, dat2, by.x="id", by.y="p.id")

1 comment:

2.5 said...

That was great, 感謝!