Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delimit a column in R based on 2 characters

I have a column in a dataframe in R that contains values such as

C22/00556,
C21/00445,
B22/00111,
C22-00679, etc.

I would like to split this into 2 columns named "initial" and "number". The delimiter being "-" or "/".
As a result I would expect a column containing C22, C21, B22, etc and another column containing 00556, 00445 etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am trying to use the separate function but I am struggling with the sep= part.
I have tried using sep= c("/","-") but this is not working and throws an error.

>Solution :

You could use separate from tidyr by / or (|) - like this:

df <- data.frame(V1 = c("C22/00556", "C21/00445", "B22/00111", "C22-00679"))

library(tidyr)
df %>%
  separate(V1, c("initial", "number"), sep = "/|-")
#>   initial number
#> 1     C22  00556
#> 2     C21  00445
#> 3     B22  00111
#> 4     C22  00679

Created on 2023-01-05 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading