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
  • 41
  • 1
  • 1
  • 5

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
  • 674,427
  • 24
  • 381
  • 486