Skip to content
Prev 10188 / 398503 Next

combine dataset

Something like this:

state1 <- c("CA","TX","FL","OR","GA","MN","NC")
count1 <- c(19,22,11,34,52,12,19)
percent1 <- c(0.34,0.35,0.24,0.42,0.62,0.17,0.34)

state2 <- c("FL","MN","CA","TX")
count2 <- c(22,22,11,52)
percent2 <- c(0.35,0.35,0.24,0.62)


data1 <- data.frame(state1,count1,percent1)
data2 <- data.frame(state2,count2,percent2)

datac <- data1
m <- match(data1$state1,data2$state2,0)
datac$count2 <- ifelse(m==0,0,data2$count2[m])
datac$percent2 <- ifelse(m==0,0,data2$percent2[m])

  If you didn't want to keep all the rows in both data sets (but just the
shared rows) you could use

 merge(data1,data2,by=1)

It might be possible to hack "merge.data.frame" to do this.
On Fri, 16 Mar 2001, Yu-Ling Wu wrote: