Skip to content

combine dataset

2 messages · Yu-Ling Wu, Ben Bolker

#
Hi,

I have two data sets look like below:

==========================
state count1 percent1
CA    19    0.34
TX    22    0.35
FL    11    0.24
OR    34    0.42
GA    52    0.62
MN    12    0.17
NC    19    0.34

state count2 percent2
FL    22    0.35
MN    22    0.35
CA    11    0.24
TX    52    0.62
==========================


How to combine these two data set and make it look
like below? Thanks in advance!

======================================
state count1 percent1 count2 percent2
CA    19    0.34      11     0.24
TX    22    0.35      52     0.62
FL    11    0.24      22     0.35
OR    34    0.42       0     0
GA    52    0.62       0     0
MN    12    0.17      22     0.35
NC    19    0.34       0     0
======================================


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
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: