How to substring the following vector (in data frame b) >b a 11 12 1234 1245 124567 126786 145769 such that: a1 a2 1 1 1 2 12 23 12 34 124 567 126 787 145 769 I tried logical commands with substr() did not work out.