Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, use regular expression to match multiple patterns and add new column to list

Tags:

regex

r

grepl

I've found numerous examples of how to match and update an entire list with one pattern and one replacement, but what I am looking for now is a way to do this for multiple patterns and multiple replacements in a single statement or loop.

Example:

> print(recs)
  phonenumber amount
1     5345091    200
2     5386052    200
3     5413949    600
4     7420155    700
5     7992284    600

I would like to insert a new column called 'service_provider' with /^5/ as Company1 and /^7/ as Company2.

I can do this with the following two lines of R:

recs$service_provider[grepl("^5", recs$phonenumber)]<-"Company1"
recs$service_provider[grepl("^7", recs$phonenumber)]<-"Company2"

Then I get:

  phonenumber amount service_provider
1     5345091    200          Company1
2     5386052    200          Company1
3     5413949    600          Company1
4     7420155    700          Company2
5     7992284    600          Company2

I'd like to provide a list, rather than discrete set of grepl's so it is easier to keep country specific information in one place, and all the programming logic in another.

thisPhoneCompanies<-list(c('^5','Company1'),c('^7','Company2'))

In other languages I would use a for loop on on the Phone Company list

For every row in thisPhoneCompanies
    Add service provider to matched entries in recs (such as the grepl statement)
end loop

But I understand that isn't the way to do it in R.

like image 259
Timothy Harding Avatar asked Sep 28 '22 13:09

Timothy Harding


1 Answers

Using stringi :

library(stringi)
recs$service_provider <- stri_replace_all_regex(str = recs$phonenumber,
                                        pattern = c('^5.*','^7.*'), 
                                        replacement = c('Company1', 'Company2'),
                                        vectorize_all = FALSE)

recs
#   phonenumber amount service_provider
# 1     5345091    200         Company1
# 2     5386052    200         Company1
# 3     5413949    600         Company1
# 4     7420155    700         Company2
# 5     7992284    600         Company2
like image 151
Dominic Comtois Avatar answered Oct 04 '22 22:10

Dominic Comtois