I have a database , I am trying to find a identifier with following conditions By taking first 3 letters from first, last 3 letters from last and last 4 digits of Phone1 if Phone1have empty cell we need to take last 4 digits of Phone2
df <- data.frame(first = c("apple", "grape", "rose", "Jasmine", "Apricots", "mango", "banana", "Blueberries"),
Last = c("Jackfruit", "Kiwi", "Mulberry", "rabbit ", "pine", "Limes", "", "Nectarine"),
Phone1 = c("1234567890", "(456)7089123", "1230789456", "", "999999", " ", "1112223334", "9998887775"),
Phone2 = c("1234737650", "", "15", "8888888888", "99", "3336783245 ", "", "2222222222"))
Expected output:
Does this work:
> library(dplyr)
> df %>% mutate(ID = case_when(Phone2 %in% c('',' ') ~ paste0(substr(first,1,3), substr(Last,1,3),substr(Phone1, nchar(Phone1)-3, nchar(Phone1))),
+ Phone1 %in% c('',' ') ~ paste0(substr(first,1,3), substr(Last,1,3),substr(Phone2, nchar(Phone2)-3, nchar(Phone2))),
+ TRUE ~ paste0(substr(first,1,3), substr(Last,1,3),substr(Phone1, nchar(Phone1)-3, nchar(Phone1)))))
first Last Phone1 Phone2 ID
1 apple Jackfruit 1234567890 1234737650 appJac7890
2 grape Kiwi (456)7089123 graKiw9123
3 rose Mulberry 1230789456 15 rosMul9456
4 Jasmine rabbit 8888888888 Jasrab8888
5 Apricots pine 999999 99 Aprpin9999
6 mango Limes 3336783245 manLim3245
7 banana 1112223334 ban3334
8 Blueberries Nectarine 9998887775 2222222222 BluNec7775
>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.