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

How to add a particular value to a particular position in rows of a specific column?

How to add a particular value to a particular position in rows of a specific column? I have this column called ID:

ID
subject01_1
subject01_2
subject01_3
...

I need to add a zero after the underline for all the subjects:

ID
subject01_01
subject01_02
subject01_03
subject01_04
... 

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

>Solution :

You can use the following code to add a 0 after the _ with gsub:

df <- read.table(text = "ID
subject01_1
subject01_2
subject01_3", header = TRUE)

df$ID <- gsub("\\_(\\d+)", "\\_0\\1", df$ID)
df
#>             ID
#> 1 subject01_01
#> 2 subject01_02
#> 3 subject01_03

Created on 2022-09-25 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