0

I am trying to subset a dataframe (n=16070) based on county name, which is coded as a string variable. The dataset looks like the following:

county         asthma eviction_rate
Alameda        10      1.2
Contra Costa   12      3.2
Orange         9       9.6
San Francisco  1       4.5

I used the following code:

state_file <- subset(state_file, state_file$county != c('Alameda', 'Contra Costa'))

When I run the code, I get the following error message:

Warning message:
In state_file$county != c("San Mateo", "San Francisco", "Contra Costa",  :
  longer object length is not a multiple of shorter object length

The output that I do get includes the aforementioned counties.

1 Answers1

0

We can use %in% and negate (!) as == is elementwise comparison, works only for a single element on the rhs of == or the same length as the lhs

subset(state_file, !county %in% c('Alameda', 'Contra Costa'))
akrun
  • 874,273
  • 37
  • 540
  • 662