R; two data sets merge by column a or column b

35 Views Asked by At

I have two data frames looking like this

view
 id object date maxdate
  1      a    8       9
  1      b    8       9
  2      a    8       9
  3      b    7       8
purchase
 id date object  purchased
  1    9      a   1
  2    8      a   1
  3    8      b   1

one is table when a product was viewed, and the other was if and when the product was purchased - after it was viewed it can be purchased within 24 hours. I want to merge them on column id, date and object OR id, maxdate=date and object, what is the best way to implement that or condition within full_join (dplyr)? below is the code for the data frame and what output I am looking for

 id object date maxdate   purchased
  1      a    8       9   1
  1      b    8       9   NA
  2      a    8       9   1
  3      b    7       8   1

id=c(1,1,2,3)
 object=c("a","b","a","b")
 date=c(8,8,8,7)
 maxdate=c(9,9,9,8)
 view=data.frame(id,object,date,maxdate)`
id=c(1,2,3)
 date=c(9,8,8)
object=c("a","a","b")
purchased=(1,1,1)
 purchase=data.frame(id,date,object,purchased)

so far I have tried something like this but it is very inefficient and confusing to clean up when it is large dataset

a=merge(view,purchase, by="id")
a$ind=ifelse(a$object.x==a$object.y & (a$date.x==a$date.y | a$maxdate==a$date.y),1,"NA")
1

There are 1 best solutions below

0
On

Are you trying to do something like this?

a=merge(view[,-4],purchase, by=c("id", "object"))
names(a) = c("id", "object", "date.viewed", "date.purchased", "purchased")

> a
  id object date.viewed date.purchased purchased
1  1      a           8              9         1
2  2      a           8              8         1
3  3      b           7              8         1