I want to use mutate()
in combination with ifelse()
and &
. However, R doesn´t realize the change but I get no error.
So, there must be a typo. This is the code I use:
library(dplyr)
dat %>%
mutate(City=ifelse(grepl("\\(030)|30|^\\+4930|(30)|^\\+49 30|^0049030|^\\+49030|0049030|^4930|^4930|^030",
`Business Phone`) & Country == "Germany", "Berlin", City))
The goal is to impute "Berlin"
if `Business Phone`
has the pattern in grepl()
and if Country
is "Germany"
.
Here is a small dput
:
structure(list(Country = c("Germany", "Germany", "Germany", "Germany",
"Germany", "Germany", "Germany", "Germany", "Germany", "Germany"
), City = c(NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), `Business Phone` = c("+49 3020618791360", "+49 (30) 24729320",
"+49 (30) 29034056", "+49 (30) 31422940", "+49 (30) 78893131",
"+49 30 2060708870", "+49 (30) 84452575", "+49 (30) 38629224",
"+49 (30) 93923158", "+49 (30) 36288666")), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))