Is this what you want:

> animal<-c("bear","bear","lion","monkey","fish","monkey","bear","zebra","zebra")
> val<-c(2,42,67,5,12,9,87,1,12)
> place<-c("S","N","N","Z","R","O","E","I","Q")
> df1<-data.frame(animal,val,place)
> animal<-c("bear","bear","lion","monkey","fish","monkey","bear","zebra","zebra")
> val<-c(21,45,78,6,18,77,89,17,28)
> place<-c("S","N","N","Z","R","G","O","P","Q")
> df2<-data.frame(animal,val,place)
> x <- merge(df1, df2, by = c('animal', 'place'), all = TRUE)
> # compute the total
> x$value <- x$val.x + x$val.y
> # separate the full/partial matches
> x[order(is.na(x$value)), ]
   animal place val.x val.y value
2    bear     N    42    45    87
4    bear     S     2    21    23
5    fish     R    12    18    30
6    lion     N    67    78   145
8  monkey     Z     5     6    11
11  zebra     Q    12    28    40
1    bear     E    87    NA    NA
3    bear     O    NA    89    NA
7  monkey     O     9    NA    NA
9  monkey     G    NA    77    NA
10  zebra     I     1    NA    NA
12  zebra     P    NA    17    NA


On Thu, Nov 15, 2012 at 9:47 AM, Geophagus <f...@retposto.net> wrote:
> Hi @ all,
> I wamnt to combine two dataframes including a condition.
> I have  two dataframes like the following:
>
> animal<-c("bear","bear","lion","monkey","fish","monkey","bear","zebra","zebra")
> val<-c(2,42,67,5,12,9,87,1,12)
> place<-c("S","N","N","Z","R","O","E","I","Q")
> df1<-data.frame(animal,val,place)
> animal<-c("bear","bear","lion","monkey","fish","monkey","bear","zebra","zebra")
> val<-c(21,45,78,6,18,77,89,17,28)
> place<-c("S","N","N","Z","R","G","O","P","Q")
> df2<-data.frame(animal,val,place)
>
> I would like to merge them with a condition. If "animal" and "place" are
> eqal in both df's, then add the values.
> If not add all three parameters ("animal","value","place") at the bottom
> (like rbind).
>
> I hope somebody can help me.
> Thank a lot.
> geo
>
>
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/merge-dataframes-with-condition-tp4649605.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to