Extracting ID from multiple strings formats using strsplit [R Tips & Tricks]

I needed to extract an ID across multiple formats and different datasets, where the possible patterns are:

ID-xx-yy, ID-xx, ID

For example:

00018523-01-02, 078-11, 789522314H

strsplit did the work:

strsplit("00018523-01-02","-")[[1]][1]
## [1] "00018523"
strsplit("078-11","-")[[1]][1]
## [1] "078"
strsplit("789522314H","-")[[1]][1]
## [1] "789522314H"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s