Check conditions in a group_by to extract a list of items

I have to confirm which clients ordered in time and paid their products in a dataframe like this:

df<-data.frame(client=c("Lucas","Muriel","Lucas","Muriel","Jack"),
               order=c("not received","not received","received","received","received"),
               payment=c("received","not received","received","received","received"))

I need to do a list of those clients that completed the two steps that my company demand: to have in the same row "not received" and "received", like Lucas did in the first row.
And to have in a different one "received" and "received", like Lucas did in the third row.

In this case, though Muriel and Jack doesnt have the first step, they are not included into the "good client list". In this case, Lucas would be the only one that completed our procedures .

I was thinking about grouping by client with group_by, and then to check if i have the two rows that i need. I need to have a list of those clients that did complete thee procedures. In tihs case, it would only have a value: "Lucas".

How can I do this?

You could do something like this.

library(dplyr, warn.conflicts = FALSE)

df <- data.frame(
  client = c("Lucas", "Muriel", "Lucas", "Muriel", "Jack"),
  order = c("not received", "not received", "received", "received", "received"),
  payment = c("received", "not received", "received", "received", "received")
)

df %>% 
  group_by(client) %>% 
  filter(n_distinct(order) > 1 & n_distinct(payment) == 1 & payment == "received") %>% 
  ungroup()
#> # A tibble: 2 x 3
#>   client order        payment 
#>   <chr>  <chr>        <chr>   
#> 1 Lucas  not received received
#> 2 Lucas  received     received

Created on 2020-11-08 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.