I am trying to create a new
variable, which would store 2nd, 3rd and 4th and the last three digits of each number of tx
. For example the first number in tx
would be 812500 in new
I did this
tx<-c(4812129004534500, 6430650557835, 579900098554, 382183224511777, 49057595473648551, 64951444098766, 649544543781, 87567909876421)
new <- sapply(tx, function(x) substring(x, first=c(2,3,4), last=3))
but the output is undesirable.
gsub()
with the regex-pattern below will work..
What is does:
It selects the parts between ()
-braces as groups.
- The first group is digits 2, 3 and 4
- The second group is the last three digits
It then replaces everything with group1 + group2
gsub( "^\\d(\\d{3}).*(\\d{3}$)", "\\1\\2", tx )
[1] "812500" "430835" "799554" "821777" "905552" "495766" "495781" "756421"