0

This is on R. I am working on looking at the rate of food insecurity in specific counties of a state. I was given data from all 50 states, and am trying to filter out all counties except the specific 14 I need. The code is not working and I am confused where I am going wrong. The code I used is this:

county_map <- MMG_2019_20_Network_2022_07_08 %>%
  group_by(State) %>%
  filter(State=="GA", `County, State`==c("Barrow County, Georgia",
         "Clarke County, Georgia", "Franklin County, Georgia", "Habersham County, Georgia",
         "Hart County, Georgia", "Jackson County, Georgia", "Madison County, Georgia",
         "Oconee County, Georgia", "Oglethorpe County, Georgia", "Rabun County, Georgia",
         "Stephens County, Georgia", "Towns County, Georgia", "White County, Georgia"))

This gives the error message

"Error in `filter()`:
! Problem while computing `..2 = `County, State` == ...`.
✖ Input `..2` must be of size 2 or 1, not size 13.'

So I understand that it's because the vector is too big, but I don't understand how to fix that. I am new to coding and don't know where to go from here.

Harrison Jones
  • 2,256
  • 5
  • 27
  • 34
  • Can you please provide the data. Read this post for details on the best way to do this. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Harrison Jones Aug 31 '22 at 13:37
  • Also, was there a reason you need to have `group_by` before the `filter`? I wouldn't expect that you need to group anything before filtering. – Harrison Jones Aug 31 '22 at 13:38

1 Answers1

1

Try using %in%; also no need to group_by:


cnties = c("Barrow County, Georgia", "Clarke County, Georgia", "Franklin County, Georgia",
           "Habersham County, Georgia", "Hart County, Georgia",
           "Jackson County, Georgia", "Madison County, Georgia", "Oconee County, Georgia",
           "Oglethorpe County, Georgia", "Rabun County, Georgia", "Stephens County, Georgia",
           "Towns County, Georgia", "White County, Georgia")

county_map <- MMG_2019_20_Network_2022_07_08 %>%
  filter(State=="GA", County %in% cnties)
langtang
  • 22,248
  • 1
  • 12
  • 27