Для этого можно использовать startsWith
и substr
(или gsub
).Во-первых, вам нужен массив с префиксами:
# variables
country_codes <- c('1', '98')
prefix <- union(country_codes, paste0('00', country_codes))
numbers <- c('00989121234567','009809121234567','989121234567','9121234567','09121234567')
# get rid of prefix
new_numbers <- character(length(numbers))
for (k in seq_along(prefix)) {
ind <- startsWith(numbers, prefix[k])
new_numbers[ind] <- substr(numbers[ind], nchar(prefix[k]) + 1, nchar(numbers[ind]))
}
new_numbers[new_numbers == ""] <- numbers[new_numbers == ""]
# results
new_numbers
# [1] "9121234567" "09121234567" "9121234567" "9121234567" "09121234567"
Затем вы можете добавить новые коды стран, например 44,31
и т. Д., Или вы также можете добавить paste0('+', country_codes)
в prefix
для работы с номерамиформа +1xxxx
.