Multiple ifelse statement with %in%

I have a dataset consisting of 1500 rows and 3 rows. I want to create a new row, that gives a name based on the data from another row.

What I have:

Bbg_Parks %>%
mutate(City = ifelse(FID_BBG2012_Clip %in% 1:4455, 
              "Rotterdam", 
ifelse(FID_BBG2012_Clip %in% 4744:5655 & 10335,
              "The_Hague",
ifelse(FID_BBG2012_Clip %in% 5861:7015,
              "Utrecht",
ifelse(FID_BBG2012_Clip %in% 7110:10190, 
              "Amsterdam", "no")))))

What I want:

Bbg_Parks %>%
  mutate(City = ifelse(FID_BBG2012_Clip %in% 1:4455 & 5736 & 10220:10330,
                "Rotterdam", 
  ifelse(FID_BBG2012_Clip %in% 4744:5655 & 10335, 
                "The_Hague",
  ifelse(FID_BBG2012_Clip %in% 5861:7015 & 10381:10385,
                "Utrecht",
  ifelse(FID_BBG2012_Clip %in% 7110:10190 & 10447:10488, 
                "Amsterdam", "no")))))

However, the last code doesn't work, as it gives me the error:

Error: Problem with mutate() input City . x unused arguments ("Rotterdam", ifelse(FID_BBG2012_Clip %in% 4744:5655, 10335, "The_Hague", ifelse(FID_BBG2012_Clip %in% 5861:7015, 10381:10385, "Utrecht", ifelse(FID_BBG2012_Clip %in% 7110:10190, 10447:10488, "Amsterdam", "no")))). i Input City is ifelse(...) . Run rlang::last_error() to see where the error occurred.

So, basically I want to include a few more data points from the columns FID_BBG2012_Clip into the newly created column "City". Could someone help a brother out?

Change this part:

FID_BBG2012_Clip %in% 1:4455 & 5736 & 10220:10330

to

FID_BBG2012_Clip %in% c(1:4455, 5736 , 10220:10330)

and repeat.

Hi. If you want to stick to ifelse() then this should work

  mutate(City = ifelse(FID_BBG2012_Clip %in% c(1:4455, 5736, 10220:10330),
                       "Rotterdam", 
                       ifelse(FID_BBG2012_Clip %in% c(4744:5655, 10335), 
                              "The_Hague",
                              ifelse(FID_BBG2012_Clip %in% c(5861:7015, 10381:10385),
                                     "Utrecht",
                                     ifelse(FID_BBG2012_Clip %in% c(7110:10190, 10447:10488), 
                                            "Amsterdam", "no")))))

However, you may want to use dplyr::case_when() which should give the same results

Bbg_Parks %>%
  mutate(City = dplyr::case_when(FID_BBG2012_Clip %in% c(1:4455, 5736, 10220:10330) ~ "Rotterdam", 
                                 FID_BBG2012_Clip %in% c(4744:5655, 10335) ~ "The_Hague",
                                 FID_BBG2012_Clip %in% c(5861:7015, 10381:10385) ~ "Utrecht",
                                 FID_BBG2012_Clip %in% c(7110:10190, 10447:10488) ~ "Amsterdam",
                                 TRUE ~ "no"))
1 Like

Perfect, thank you very much!
Just a small question though for my interest, what is the diff between case_when and ifelse?

Essentially case_when() is just more convenient. You don't need to repeatedly write ifesle() ifelse() ifelse() and it makes code easier to read.
From documentation

Description
This function allows you to vectorise multiple if_else() statements. It is an R equivalent of the SQL CASE WHEN statement. If no cases match, NA is returned.

1 Like

Thanks and have a great day!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.