订购电话号码

时间:2018-11-20 06:40:59

标签: r regex stringi

我有一个像下面这样的数据集,电话号码用不同的数字和格式表示。

您能帮我使用R将它们订购为标准格式吗?

TelephoneData <- data.frame(
  FIRST = c("STAN", "FIONA", "JOHN", "VERA", "ROBERT", "ANGIE", "PAUL", "GEORGE", "JUDITH", "TREVOR", "KEN", "BRIAN", "GLADYS", "MARY", "MARY", "JOSHUA", 
            "BRIAN", "PHILLIP", "KATE", "BRIAN"),
  PHONE = c("+44 1152 195298", "07366 602865", "01160 979447", "01597 501161", "01232 637283", "01296 230679", "(07183) 151418", "(07995) 376450", 
            "(0208) 0511522", "+44 208 3960687", "(01544) 668176", "(07540) 940315", "0208 4137611", "(01472) 119737", "(0208) 6494623", 
            "(01156) 145807", "07731 566115", "(0207) 7270589", "(0207) 7542812", "(01205) 835056")
  )

2 个答案:

答案 0 :(得分:2)

假设您的数据帧称为data,您可以像这样清理电话号码:

 library(stringi)
 data$PHONENUM <- stri_replace_all_fixed(data$PHONENUM, '+44', '0') #changes +44 to 0
 data$PHONENUM <- gsub("[^0-9.]", "", data$PHONENUM) # removes all white space and ()

然后您可以按以下方式订购电话号码:

 data[order(data$PHONENUM), ]

您需要做什么吗?

编辑:根本不需要lapply,这些功能仍然可以处理整个列表

答案 1 :(得分:1)

这也可能有用:

TelephoneData$TelNr <- gsub("\\+44", "0", gsub("[() ]", "", TelephoneData$PHONE))   #replace +44 by 0, remove spaces and brackets
TelephoneData$TelNr <- gsub("([0-9]{5})(.*)", "\\1 \\2", TelephoneData$TelNr) #insert space after every 5 chars
TelephoneData <- TelephoneData[order(TelephoneData$TelNr ),] #sort by the column TelNr

给出结果

#     FIRST           PHONE        TelNr
#1     STAN +44 1152 195298 01152 195298
#16  JOSHUA  (01156) 145807 01156 145807
#3     JOHN    01160 979447 01160 979447
#20   BRIAN  (01205) 835056 01205 835056
#5   ROBERT    01232 637283 01232 637283
#6    ANGIE    01296 230679 01296 230679
#14    MARY  (01472) 119737 01472 119737
#11     KEN  (01544) 668176 01544 668176
#4     VERA    01597 501161 01597 501161
#18 PHILLIP  (0207) 7270589 02077 270589
#19    KATE  (0207) 7542812 02077 542812
#9   JUDITH  (0208) 0511522 02080 511522
#10  TREVOR +44 208 3960687 02083 960687
#13  GLADYS    0208 4137611 02084 137611
#15    MARY  (0208) 6494623 02086 494623
#7     PAUL  (07183) 151418 07183 151418
#2    FIONA    07366 602865 07366 602865
#12   BRIAN  (07540) 940315 07540 940315
#17   BRIAN    07731 566115 07731 566115
#8   GEORGE  (07995) 376450 07995 376450

希望这会有所帮助!