Ordering telephone numbers












-1















I have a dataset like below and the telephone numbers are in different digits and formats.



Would you help me ordering them into a standard format using 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")
)









share|improve this question




















  • 1





    Can you edit the question to include data we can copy-paste into R? Try using dput(data) and pasting that into the question. Make sure to indent all code lines by 5 spaces so they appear as code as well, this will make your answers more readable

    – RAB
    Nov 20 '18 at 6:42








  • 1





    Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 20 '18 at 6:43











  • So, what have you tried?

    – Andreas
    Nov 20 '18 at 6:44











  • @user10626943 I edited the data but is it in a form that you can paste it into a normal form?

    – dido
    Nov 20 '18 at 7:07











  • Not really, I'm just bored so I pasted into notpead, pressed enter before all 'IDxxx', saved it as a csv and read it in :P. Tbh your original data was better than what you have now... what you should do in future is dput() the whole dataframe and paste the output here. Also remember to indent as then it gets that nice grey background

    – RAB
    Nov 20 '18 at 7:09


















-1















I have a dataset like below and the telephone numbers are in different digits and formats.



Would you help me ordering them into a standard format using 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")
)









share|improve this question




















  • 1





    Can you edit the question to include data we can copy-paste into R? Try using dput(data) and pasting that into the question. Make sure to indent all code lines by 5 spaces so they appear as code as well, this will make your answers more readable

    – RAB
    Nov 20 '18 at 6:42








  • 1





    Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 20 '18 at 6:43











  • So, what have you tried?

    – Andreas
    Nov 20 '18 at 6:44











  • @user10626943 I edited the data but is it in a form that you can paste it into a normal form?

    – dido
    Nov 20 '18 at 7:07











  • Not really, I'm just bored so I pasted into notpead, pressed enter before all 'IDxxx', saved it as a csv and read it in :P. Tbh your original data was better than what you have now... what you should do in future is dput() the whole dataframe and paste the output here. Also remember to indent as then it gets that nice grey background

    – RAB
    Nov 20 '18 at 7:09
















-1












-1








-1


1






I have a dataset like below and the telephone numbers are in different digits and formats.



Would you help me ordering them into a standard format using 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")
)









share|improve this question
















I have a dataset like below and the telephone numbers are in different digits and formats.



Would you help me ordering them into a standard format using 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")
)






r regex stringi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 8:39









Patrik_P

1,93721225




1,93721225










asked Nov 20 '18 at 6:40









didodido

147




147








  • 1





    Can you edit the question to include data we can copy-paste into R? Try using dput(data) and pasting that into the question. Make sure to indent all code lines by 5 spaces so they appear as code as well, this will make your answers more readable

    – RAB
    Nov 20 '18 at 6:42








  • 1





    Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 20 '18 at 6:43











  • So, what have you tried?

    – Andreas
    Nov 20 '18 at 6:44











  • @user10626943 I edited the data but is it in a form that you can paste it into a normal form?

    – dido
    Nov 20 '18 at 7:07











  • Not really, I'm just bored so I pasted into notpead, pressed enter before all 'IDxxx', saved it as a csv and read it in :P. Tbh your original data was better than what you have now... what you should do in future is dput() the whole dataframe and paste the output here. Also remember to indent as then it gets that nice grey background

    – RAB
    Nov 20 '18 at 7:09
















  • 1





    Can you edit the question to include data we can copy-paste into R? Try using dput(data) and pasting that into the question. Make sure to indent all code lines by 5 spaces so they appear as code as well, this will make your answers more readable

    – RAB
    Nov 20 '18 at 6:42








  • 1





    Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

    – Ronak Shah
    Nov 20 '18 at 6:43











  • So, what have you tried?

    – Andreas
    Nov 20 '18 at 6:44











  • @user10626943 I edited the data but is it in a form that you can paste it into a normal form?

    – dido
    Nov 20 '18 at 7:07











  • Not really, I'm just bored so I pasted into notpead, pressed enter before all 'IDxxx', saved it as a csv and read it in :P. Tbh your original data was better than what you have now... what you should do in future is dput() the whole dataframe and paste the output here. Also remember to indent as then it gets that nice grey background

    – RAB
    Nov 20 '18 at 7:09










1




1





Can you edit the question to include data we can copy-paste into R? Try using dput(data) and pasting that into the question. Make sure to indent all code lines by 5 spaces so they appear as code as well, this will make your answers more readable

– RAB
Nov 20 '18 at 6:42







Can you edit the question to include data we can copy-paste into R? Try using dput(data) and pasting that into the question. Make sure to indent all code lines by 5 spaces so they appear as code as well, this will make your answers more readable

– RAB
Nov 20 '18 at 6:42






1




1





Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

– Ronak Shah
Nov 20 '18 at 6:43





Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you.

– Ronak Shah
Nov 20 '18 at 6:43













So, what have you tried?

– Andreas
Nov 20 '18 at 6:44





So, what have you tried?

– Andreas
Nov 20 '18 at 6:44













@user10626943 I edited the data but is it in a form that you can paste it into a normal form?

– dido
Nov 20 '18 at 7:07





@user10626943 I edited the data but is it in a form that you can paste it into a normal form?

– dido
Nov 20 '18 at 7:07













Not really, I'm just bored so I pasted into notpead, pressed enter before all 'IDxxx', saved it as a csv and read it in :P. Tbh your original data was better than what you have now... what you should do in future is dput() the whole dataframe and paste the output here. Also remember to indent as then it gets that nice grey background

– RAB
Nov 20 '18 at 7:09







Not really, I'm just bored so I pasted into notpead, pressed enter before all 'IDxxx', saved it as a csv and read it in :P. Tbh your original data was better than what you have now... what you should do in future is dput() the whole dataframe and paste the output here. Also remember to indent as then it gets that nice grey background

– RAB
Nov 20 '18 at 7:09














2 Answers
2






active

oldest

votes


















1














This might be useful as well:



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


Giving the result



#     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


Hope this helps!






share|improve this answer





















  • 1





    I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

    – dido
    Nov 20 '18 at 7:46













  • and did u get what u wanted?

    – Patrik_P
    Nov 20 '18 at 7:47











  • Unfortunately, no. It doesn't give me list of telephone numbers.

    – dido
    Nov 20 '18 at 7:50











  • Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

    – Patrik_P
    Nov 20 '18 at 7:55













  • I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

    – dido
    Nov 20 '18 at 8:01





















2














Assuming your dataframe is called data you can clean up the phone numbers like this:



 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 ()


Then you can order the phone numbers like this:



 data[order(data$PHONENUM), ]


Does that do what you need?



EDIT: don't need the lapply at all, those functions will do the whole list anyway






share|improve this answer


























  • Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

    – dido
    Nov 20 '18 at 7:18













  • The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

    – dido
    Nov 20 '18 at 7:31











  • It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

    – RAB
    Nov 20 '18 at 7:34











  • I use the version of R 3.5.1

    – dido
    Nov 20 '18 at 7:41











  • hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

    – RAB
    Nov 20 '18 at 7:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387558%2fordering-telephone-numbers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This might be useful as well:



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


Giving the result



#     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


Hope this helps!






share|improve this answer





















  • 1





    I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

    – dido
    Nov 20 '18 at 7:46













  • and did u get what u wanted?

    – Patrik_P
    Nov 20 '18 at 7:47











  • Unfortunately, no. It doesn't give me list of telephone numbers.

    – dido
    Nov 20 '18 at 7:50











  • Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

    – Patrik_P
    Nov 20 '18 at 7:55













  • I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

    – dido
    Nov 20 '18 at 8:01


















1














This might be useful as well:



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


Giving the result



#     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


Hope this helps!






share|improve this answer





















  • 1





    I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

    – dido
    Nov 20 '18 at 7:46













  • and did u get what u wanted?

    – Patrik_P
    Nov 20 '18 at 7:47











  • Unfortunately, no. It doesn't give me list of telephone numbers.

    – dido
    Nov 20 '18 at 7:50











  • Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

    – Patrik_P
    Nov 20 '18 at 7:55













  • I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

    – dido
    Nov 20 '18 at 8:01
















1












1








1







This might be useful as well:



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


Giving the result



#     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


Hope this helps!






share|improve this answer















This might be useful as well:



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


Giving the result



#     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


Hope this helps!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 9:27

























answered Nov 20 '18 at 7:22









Patrik_PPatrik_P

1,93721225




1,93721225








  • 1





    I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

    – dido
    Nov 20 '18 at 7:46













  • and did u get what u wanted?

    – Patrik_P
    Nov 20 '18 at 7:47











  • Unfortunately, no. It doesn't give me list of telephone numbers.

    – dido
    Nov 20 '18 at 7:50











  • Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

    – Patrik_P
    Nov 20 '18 at 7:55













  • I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

    – dido
    Nov 20 '18 at 8:01
















  • 1





    I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

    – dido
    Nov 20 '18 at 7:46













  • and did u get what u wanted?

    – Patrik_P
    Nov 20 '18 at 7:47











  • Unfortunately, no. It doesn't give me list of telephone numbers.

    – dido
    Nov 20 '18 at 7:50











  • Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

    – Patrik_P
    Nov 20 '18 at 7:55













  • I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

    – dido
    Nov 20 '18 at 8:01










1




1





I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

– dido
Nov 20 '18 at 7:46







I tried your code by adding TelephoneData$ into PHONEMUM as; sortedTelNr <- sort(gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM))) but it turns out as ; +44s*

– dido
Nov 20 '18 at 7:46















and did u get what u wanted?

– Patrik_P
Nov 20 '18 at 7:47





and did u get what u wanted?

– Patrik_P
Nov 20 '18 at 7:47













Unfortunately, no. It doesn't give me list of telephone numbers.

– dido
Nov 20 '18 at 7:50





Unfortunately, no. It doesn't give me list of telephone numbers.

– dido
Nov 20 '18 at 7:50













Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

– Patrik_P
Nov 20 '18 at 7:55







Lets say you got a data.frame data. You got more columns there including the PHONENUM. Then you can do data$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", data$PHONENUM)) and then sort it like data <- data[order(data$TelNr ),]

– Patrik_P
Nov 20 '18 at 7:55















I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

– dido
Nov 20 '18 at 8:01







I entered it as (my data.frame is called as TelephoneData) TelephoneData$TelNr <- gsub("\+44\s*", "0", gsub("[()]", "", TelephoneData$PHONENUM)) I received this warning message; In base::gsub(pattern = pattern, replacement = replacement, x = x, : argument 'replacement' has length > 1 and only the first element will be used What should I do? The PHONENUM column's elements in all rows turned into +44s .

– dido
Nov 20 '18 at 8:01















2














Assuming your dataframe is called data you can clean up the phone numbers like this:



 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 ()


Then you can order the phone numbers like this:



 data[order(data$PHONENUM), ]


Does that do what you need?



EDIT: don't need the lapply at all, those functions will do the whole list anyway






share|improve this answer


























  • Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

    – dido
    Nov 20 '18 at 7:18













  • The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

    – dido
    Nov 20 '18 at 7:31











  • It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

    – RAB
    Nov 20 '18 at 7:34











  • I use the version of R 3.5.1

    – dido
    Nov 20 '18 at 7:41











  • hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

    – RAB
    Nov 20 '18 at 7:42
















2














Assuming your dataframe is called data you can clean up the phone numbers like this:



 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 ()


Then you can order the phone numbers like this:



 data[order(data$PHONENUM), ]


Does that do what you need?



EDIT: don't need the lapply at all, those functions will do the whole list anyway






share|improve this answer


























  • Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

    – dido
    Nov 20 '18 at 7:18













  • The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

    – dido
    Nov 20 '18 at 7:31











  • It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

    – RAB
    Nov 20 '18 at 7:34











  • I use the version of R 3.5.1

    – dido
    Nov 20 '18 at 7:41











  • hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

    – RAB
    Nov 20 '18 at 7:42














2












2








2







Assuming your dataframe is called data you can clean up the phone numbers like this:



 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 ()


Then you can order the phone numbers like this:



 data[order(data$PHONENUM), ]


Does that do what you need?



EDIT: don't need the lapply at all, those functions will do the whole list anyway






share|improve this answer















Assuming your dataframe is called data you can clean up the phone numbers like this:



 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 ()


Then you can order the phone numbers like this:



 data[order(data$PHONENUM), ]


Does that do what you need?



EDIT: don't need the lapply at all, those functions will do the whole list anyway







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 7:23

























answered Nov 20 '18 at 7:05









RABRAB

838117




838117













  • Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

    – dido
    Nov 20 '18 at 7:18













  • The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

    – dido
    Nov 20 '18 at 7:31











  • It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

    – RAB
    Nov 20 '18 at 7:34











  • I use the version of R 3.5.1

    – dido
    Nov 20 '18 at 7:41











  • hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

    – RAB
    Nov 20 '18 at 7:42



















  • Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

    – dido
    Nov 20 '18 at 7:18













  • The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

    – dido
    Nov 20 '18 at 7:31











  • It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

    – RAB
    Nov 20 '18 at 7:34











  • I use the version of R 3.5.1

    – dido
    Nov 20 '18 at 7:41











  • hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

    – RAB
    Nov 20 '18 at 7:42

















Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

– dido
Nov 20 '18 at 7:18







Well I should fix the phone numbers' format as below; from this format: (01119) 963864 into this: 01119 963864, from this format: (01142) 262574 into this: 01142 262574

– dido
Nov 20 '18 at 7:18















The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

– dido
Nov 20 '18 at 7:31





The first code changed the numbers, but when I enter the second code it doesn't give me any results as a list.

– dido
Nov 20 '18 at 7:31













It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

– RAB
Nov 20 '18 at 7:34





It should just remove the white space and the brackets. It works for me, what version of R are you running? (use session_info() to help get that)

– RAB
Nov 20 '18 at 7:34













I use the version of R 3.5.1

– dido
Nov 20 '18 at 7:41





I use the version of R 3.5.1

– dido
Nov 20 '18 at 7:41













hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

– RAB
Nov 20 '18 at 7:42





hmm, me too. Maybe I've just misunderstood what you want as it seems to all work the way I would want it to

– RAB
Nov 20 '18 at 7:42


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387558%2fordering-telephone-numbers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$