'Want to find matching strings within a column, by group, and condition

I have a list of a few companies with each line as a deal. My table indicates whether they had a Initial Public Offering (IPO) or not, and what bank lead the deal:

df <- data.frame(Issuer=c("ABC Inc.", "ABC Inc.","ABC Inc.", "XYZ Co.","XYZ Co","123 Inc.","123 Inc."),
             IPO=c("Yes","No","No","Yes","No","Yes","No"),
             Bank =c("RBC","RBC","CIBC","RBC","Goldman","Citi","Citi"))

I want to create a list of all Issuers where they have the same Bank when IPO=="Yes" AND IPO=="No" like so:

Issuer Bank
ABC Inc. RBC
123 Inc. Citi

I've been circling the problem like this:

temp = df %>% group_by(Issuer) %>% filter(Bank[IPO=="Yes"] == Bank[IPO=="No"])

Thank you!

r


Solution 1:[1]

Here's another way -

library(dplyr)

df %>% 
  group_by(Issuer, Bank) %>%
  filter(all(c('Yes', 'No') %in% IPO)) %>%
  ungroup %>%
  distinct(Issuer, Bank)

#  Issuer   Bank 
#  <chr>    <chr>
#1 ABC Inc. RBC  
#2 123 Inc. Citi 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronak Shah