0

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"))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
Banjo
  • 1,191
  • 1
  • 11
  • 28
  • 1
    Hi, please [provide data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), e.g. adding the output of `dput()` or `dput(head())` to your question. You'll have a much better chance of getting a great answer! – jay.sf Feb 11 '19 at 12:34
  • 1
    I don't get an error... – s_baldur Feb 11 '19 at 12:50
  • Yes, I also doesn´t get an error but R doesn't change the values. For example, if a number starts with "+49" the NA value remains and "Berlin will not be imputed. – Banjo Feb 11 '19 at 13:03

1 Answers1

1

You could use regular expressions and conditional assignment.

dat$City[grepl("^\\+?(0?0)?(49)0?\\s?\\(?(30).+|^\\(?(0?(30)).+", 
      dat$`Business Phone`) & dat$Country == "Germany"] <- "Berlin"

> dat
# A tibble: 10 x 3
   Country City   `Business Phone` 
   <chr>   <chr>  <chr>            
 1 Germany Berlin +49 3020618791360
 2 Germany Berlin +49 (30) 24729320
 3 Germany Berlin +49 (30) 29034056
 4 Germany Berlin +49 (30) 31422940
 5 Germany Berlin +49 (30) 78893131
 6 Germany Berlin +49 30 2060708870
 7 Germany Berlin +49 (30) 84452575
 8 Germany Berlin +49 (30) 38629224
 9 Germany Berlin +49 (30) 93923158
10 Germany Berlin +49 (30) 36288666

Test with this data:

nums <- c("+49 (30) 78893131", "+49 30 2060708870", "+42 (30) 36288666 ", 
"+19 (30) 36288666 ", "+49 (20) 36288666", "30456745674", "+493045674567", 
"(30)45674567", "+49 3045674567", "004903045674567", "+4903045674567", 
"004903045674567", "493045674567", "493045674567", "03045674567", 
"+49 (20) 36288666", "004920000", "204054756", "3145675678", 
"49403235678", "49030345435", "20456745674", "+193045674567", 
"(20)45674567", "+41 3045674567", "004103045674567", "+4103045674567", 
"004104945674567", "413045674567", "413045674567", "01045674567"
)

> nums[grepl("^\\+?(0?0)?(49)0?\\s?\\(?(30).+|^\\(?(0?(30)).+", nums)]
 [1] "+49 (30) 78893131" "+49 30 2060708870" "30456745674"       "+493045674567"    
 [5] "(30)45674567"      "+49 3045674567"    "004903045674567"   "+4903045674567"   
 [9] "004903045674567"   "493045674567"      "493045674567"      "03045674567"      
[13] "49030345435"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • _Note:_ The code can easily be adapted to other area codes. More about regular expressions you can find e.g. [here](https://regexone.com/). – jay.sf Feb 12 '19 at 10:20