0

I want to be able to keep all rows where the "conm" column does contain certain bank names. you can tell from the code I am trying to use subset to do this but to no avail.

I have tried using subset to do this.

 CMPSTPRFT12 <- subset(CMPSPRFT11, conm = MORGUARD CORP | conm = LEHMAN BROTHERS HOLDINGS INC)

I expect the output in rstudio to just show all rows where the column containing the names of banks includes certain banks, not all banks. I want SUnTrust, Lehman Brothers, Morgan Stanley, Goldman Sachs, PennyMac, Bank of America, and Fannie Mae.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • 1
    `==` is what you want, not `=` The `=` is used for assignment like `<-`, while `==` is used for comparison. – thelatemail Jul 17 '19 at 00:40
  • 1
    Also, you have to quote your bank names to say they are strings - e.g.: `subset(CMPSPRFT11, conm == "MORGUARD CORP" | conm == "LEHMAN BROTHERS HOLDINGS INC")` – thelatemail Jul 17 '19 at 00:48

1 Answers1

0

Please see other posts on how to phrase your questions more helpfully for others. How to make a great R reproducible example

You can use dplyr and filter.

df <- data.frame(bank=letters[1:10],
                 value=10:19)

df %>% filter(bank=='a' | bank=='b')

  bank value
1    a    10
2    b    11

banks <- c('d','g','j')
df %>% filter(bank %in% banks)

  bank value
1    d    13
2    g    16
3    j    19
kstew
  • 1,104
  • 6
  • 21