I have two datasets: dataset1
and dataset2
.
zz <- "id_customer id_order order_date
1 1 2018-10
1 2 2018-11
2 3 2019-05
3 4 2019-06"
dataset1 <- read.table(text=zz, header=TRUE)
yy <- "id_customer order_date
1 2018-10
3 2019-06"
dataset2 <- read.table(text=yy, header=TRUE)
dataset2
is the result of a query where I have two columns: id_customer
and date
(format YYYY-mm).
Those correspond to customers which have a different status than the others in the source dataset (dataset1
), for a specified month.
dataset1
is a list of transactions where I have id_customer
, id_order
and date
(format YYYY-mm as well).
I want to enrich dataset1
with a "flag" column for each line set to 1 if the customer id appears in dataset2
, during the corresponding month.
I have tried something as follows:
dataset$flag <- ifelse(dataset1$id_customer %in% dataset2$id_customer &
dataset1$date == dataset2$date,
"1", "0")
But I get a warning message that says 'longer object length is not a multiple of shorter object length'. I understand that but cannot come up with a solution. Could someone please help?