1

I have some data, now I have to count the number of transitions from alphabetic to numeric OR from numeric to alphabetic.

dd <- c(text="S4FDD-S4DF5D_S54F4SDF4","eDC54E_EG5SF3543+TDX32RF","CVB5+5VN7NLC2_3LM70LCM8","1VPLF3LPD5P6OK7POD8KP9OASD9POA0")

Eg. in dd[1]:

S4FDD-S4DF5D_S54F4SDF4 == 6 (Alphabetic to number Count)
                                          & 
                       == 5 (Number to Alphabetic Count)

I tried by using this function, but failed :

stri_count_boundaries(dd, type="character")
Jaap
  • 81,064
  • 34
  • 182
  • 193

2 Answers2

3

Here is a base R option using lengths + gregexpr:

> lengths(gregexpr("[[:alpha:]]\\d+",dd))
[1] 6 4 5 8

> lengths(gregexpr("\\d+[[:alpha:]]",dd))
[1] 5 3 4 8
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • 1
    There is no need for `regmatches`: `lengths(gregexpr("[[:alpha:]]\\d", dd))` and `lengths(gregexpr("\\d[[:alpha:]]", dd))` will do it already. – GKi Jul 22 '20 at 09:42
1

If I have understood you correctly you want to count how many times you encounter an alphabet followed by a number and vice - versa.

You can use str_count here :

library(stringr)
str_count(dd, '[A-Z]\\d')
#[1] 6 4 5 8

str_count(dd, '\\d[A-Z]')
#[1] 5 3 4 8
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213