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.