I have customer id, product_id1 and product_id2. The data have customers' purchasing information and sorted by customer and time so 1st row of each customer is the oldest record.
product_id1 includes purchased items
product_id2 includes items that I want to know whether each of item is purchased before (check from the product_id1).
For each item in product_id2 for each customer, I want to create dummy variable that indicated whether each of item in product_id2 is purchased in the past
In other words, a value in product_id2 at nth row has been shown at least one time in 1~(n-1)th rows in product_id1, the purchased_before is 1 otherwise 0.
So I want to create "purchased_before" column.
I can do it by using for loop but is there any efficient way?
Data is as below,
customer id product_id1 product_id2 purchased_before
1 112 113 0
1 115 114 0
1 113 113 0
1 114 113 1
1 115 114 1
....
2 112 115 0
2 115 112 1
2 113 113 0