Murali
Murali

Reputation: 601

Filter Data frame using 3 different vector conditions

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.

  1. If all my periods (1, 2 and 3) meet “findme” value is equal to ‘Zero’ and
    1. if that happens to the same product and
    2. and same account.

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

Answers (2)

akrun
akrun

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

Z.Lin
Z.Lin

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

Related Questions