0

I found a similar question involving templates that was was beyond the scope of my question. I want to be able to say something like: if (a and b) then (do something). Here is an example:

t1 <- tribble(
    ~state,     ~county,
    "New York", "Bronx",
    "New York", "Richmond",
    "New York", "Albany",
    "Virginia", "Richmond"
    )

five_boroughs = c("Bronx", "Kings", "New York", "Queens", "Richmond")

if t1$state == "New York" && t1$county in five_boroughs
    t1$county = "New York City"

Using either &, &&, in, or %in% puts New York City in Virginia. I apologize to New Yorkers for calling counties boroughs.

SteveM49
  • 61
  • 1
  • 1
  • 5
  • 1
    `t1$county <- dplyr::if_else(t1$state == "New York" & t1$count %in% five_boroughs, "New York City", NA_character)` – r2evans Apr 17 '20 at 22:19
  • This is likely a duplicate of https://stackoverflow.com/q/6558921/3358272 and/or others. – r2evans Apr 17 '20 at 22:20

1 Answers1

0

We can use case_when

library(dplyr)
library(stringr)
t1 %>% 
   mutate(county = case_when(state == 'New York' & 
          county %in% five_boroughs~ str_c(state, ' City')))
akrun
  • 874,273
  • 37
  • 540
  • 662