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
...
>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