Reputation: 601
My sample dataset
df <- data.frame(period=rep(1:3,3),
product=c(rep('A',9)),
account= c(rep('1001',3),rep('1002',3),rep('1003',3)),
findme= c(0,0,0,1,0,1,4,2,0))
My Desired output dataset
output <- data.frame(period=rep(1:3,2),
product=c(rep('A',6)),
account= c(rep('1002',3),rep('1003',3)),
findme= c(1,0,1,4,2,0))
Here my conditions are.... I want to eliminate records 3 records from 9 based on below conditions.
Rule 1: It should meet Periods 1, 2, 3 Rule 2: Findme value for all periods = 0 Rule 3: All those 3 records (Preiod 1,2,3) should have same Product Rule 4: All those 3 recods (period 1,2,3) should have one account.
Upvotes: 0
Views: 29
Reputation: 886948
Here is an option with data.table
library(data.table)
setDT(df)[df[, .I[all(1:3 %in% period) & !all(!findme)], .(product, account)]$V1]
# period product account findme
#1: 1 A 1002 1
#2: 2 A 1002 0
#3: 3 A 1002 1
#4: 1 A 1003 4
#5: 2 A 1003 2
#6: 3 A 1003 0
Upvotes: 1
Reputation: 29075
If I understand correctly, you want to drop all records from a product-account combination where findme == 0, if all periods are included in this combination?
library(dplyr)
df %>%
group_by(product, account, findme) %>%
mutate(all.periods = all(1:3 %in% period)) %>%
ungroup() %>%
filter(!(findme == 0 & all.periods)) %>%
select(-all.periods)
# A tibble: 6 x 4
period product account findme
<int> <fctr> <fctr> <dbl>
1 1 A 1002 1
2 2 A 1002 0
3 3 A 1002 1
4 1 A 1003 4
5 2 A 1003 2
6 3 A 1003 0
Upvotes: 2