Reputation: 77
I have this dataframe in R
raw_payment_id from_bank_account amount posted_at
<int> <chr> <dbl> <date>
1 620691 SK660900000000062087 20.0 2018-02-25
2 618433 SK660900000000062087 10.0 2018-02-27
3 623157 SK660900000000062087 10.0 2018-03-02
4 628236 SK300900000000506871 812. 2018-03-06
5 627899 SK300900000000506871 812. 2018-03-07
6 628966 SK660900000000062087 10.0 2018-03-09
My goal is to find if payment from same account and at same amount was posted within 3 days. And if yes, mark both payments with 1. So result would be.
raw_payment_id from_bank_account amount posted_at test
<int> <chr> <dbl> <date> <int>
1 620691 SK660900000000062087 20.0 2018-02-25 0
2 618433 SK660900000000062087 10.0 2018-02-27 1
3 623157 SK660900000000062087 10.0 2018-03-02 1
4 628236 SK300900000000506871 812. 2018-03-06 1
5 627899 SK300900000000506871 812. 2018-03-07 1
6 628966 SK660900000000062087 10.0 2018-03-09 0
I can´t find a way how to do that, my tryings with lag/leads fails on fact that there could be only one payment from bank account.
Upvotes: 0
Views: 206
Reputation: 23231
library(dplyr)
# Within each accounts, how many transactions were the same amount
tmp <- mydat %>%
group_by(from_bank_account, amount) %>%
mutate(number_of_dupes = n()) %>%
filter(number_of_dupes > 1) # only keep duplicates
# remove dups > 3 days apart
tmp$dup <- 0
for(i in 1:nrow(tmp)){
acct <- tmp$from_bank_account[i]
n <- tmp$number_of_dupes[i]
if(length(tmp$dup[(abs(difftime(tmp$posted_at[i],tmp$posted_at,units = "days")) < 4)
& (tmp$from_bank_account == acct)]) > 1){
tmp$dup[i] <- 1
}
}
tmp <- tmp[tmp$dup==1,]
mydat$flag_duplicate <- ifelse(mydat$raw_payment_id %in% tmp$raw_payment_id,1,0)
raw_payment_id from_bank_account amount posted_at flag_duplicate 1 620691 SK660900000000062087 20 2018-02-25 0 2 618433 SK660900000000062087 10 2018-02-27 1 3 623157 SK660900000000062087 10 2018-03-02 1 4 628236 SK300900000000506871 812 2018-03-06 1 5 627899 SK300900000000506871 812 2018-03-07 1 6 628966 SK660900000000062087 10 2018-03-09 0
Upvotes: 0
Reputation: 6132
library(dplyr)
df %>%
group_by(from_bank_account, amount) %>%
mutate(var = case_when(abs(as.Date(posted_at) - as.Date(lag(posted_at))) < 4 ~ 1,
abs(as.Date(posted_at) - as.Date(lead(posted_at))) < 4 ~ 1,
TRUE ~ 0))
raw_payment_id from_bank_account amount posted_at var
<int> <fct> <dbl> <fct> <dbl>
1 620691 SK660900000000062087 20. 2018-02-25 0.
2 618433 SK660900000000062087 10. 2018-02-27 1.
3 623157 SK660900000000062087 10. 2018-03-02 1.
4 628236 SK300900000000506871 812. 2018-03-06 1.
5 627899 SK300900000000506871 812. 2018-03-07 1.
6 628966 SK660900000000062087 10. 2018-03-09 0.
Upvotes: 2