R string replacement: telephone number processing

Source: Internet
Author: User

Problem:

Now, I have a column of phone numbers:

12554189525

15527237602

15376581027

I want ~ 8 digits are replaced by *. How can this problem be solved?


Answer:

X <-as. character (C (12554189525,15527237602, 15376581027) substr (x, 5, 8) <-'*****' x

This is simple and efficient.


X <-. character (C (12554189525,15527237602, 15376581027) (Y <-paste (substr (x,), '***', substring (x, 9 ), SEP = ''))

This uses two substr and one paste, and the computing complexity increases a lot, resulting in poor performance.


X <-. character (C (12554189525,15527237602, 15376581027) for (I in 1: length (x) {x [I] <-sub (substr (X [I], 5, 8 ), "*****", X [I])} X

This method uses a loop, and the sub and substr functions are used in the loop. The efficiency is poor. This is still a traditional programming idea, and the vector programming style of R is not considered at all. The first answer is the vector programming style.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.