1

/I have a data frame as such:

customer_id<-c("aks123","epfk123","jfk1234","ekdm453","ddd4356","epfk123","ffg234","aks123","jfk1234","ekdm453")
customer_type<-c("loyal","good","old","new","new","loyal","good","loyal","good","new")
df<-data.frame(customer_id,customer_type)

I want to filter the data such that I want unique customer_id's only if their respective customer_type are same, if the customer types are different, I want them to be shown as two separate rows.

There are many other columns in the df as well (like name,budget,...), these are just the ones I want to put the filter based on. So using unique function would be an issue.

For example customer_id "jfk1234" is twice but has customer type both "loyal" and "good", so they should be in separate rows but customer_id "aks123" also appearing twice, is just in customer_type "loyal", and so should be only once

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • `unique(df)` .. ? Or `dplyr::distinct(df)` – Ronak Shah Sep 02 '21 at 03:59
  • @RonakShah sorry, I forgot to mention there are many other columns in the df as well, these are just the ones I want to put the filter on. So using unique function would be an issue as it would work on the entire data frame – hereforadoubt Sep 02 '21 at 04:02
  • 1
    You can use `dplyr::distinct(df, customer_id, customer_type, .keep_all = TRUE)` to get unique row based on two columns but keeping all the columns in the output. – Ronak Shah Sep 02 '21 at 04:03

1 Answers1

1

You can try

df %>%
  group_by(customer_id, customer_type) %>%
  unique

  customer_id customer_type
  <chr>       <chr>        
1 aks123      loyal        
2 epfk123     good         
3 jfk1234     old          
4 ekdm453     new          
5 ddd4356     new          
6 epfk123     loyal        
7 ffg234      good         
8 jfk1234     good
Park
  • 14,771
  • 6
  • 10
  • 29