Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R : find a pattern and edit

I am aware of all of the questions regarding the Adding leading zero and the comprehensive responses provided for them such as Q1, Q2, Q3.

But to me , at least based on my current knowledge, I am not able to address what I am going to do as follow:

  • add the leading zero in a string using regex pattern match So, I want to add leading zero only to digits after the -.


for example :

Sam <- c("222-88", "537-457", "652-1", "787-892")
var <- LETTERS[1:4]
DF<- data.frame(Sam, var)
DF
      Sam var
1  222-88   A
2  537-457  B
3  652-1    C
4  787-892  D

Expected results:

     Sam   var
1  222-088   A
2  537-457   B
3  652-001   C
4  787-892   D

I tried :

library(stringr)
temp <- DF[str_detect(DF$Sam, "-[0-9]{1,2}$"),] # will find the rows need the leading zero
temp 
     Sam var
1 222-88   A
3  652-1   C

formatC(temp$Sam, width = 2,flag = 0)# not correct!
like image 672
Daniel Avatar asked Feb 26 '26 23:02

Daniel


2 Answers

We can do this with base R, by splitting the string by - and then use sprintf to pad the 0's after converting to numeric and then paste

DF$Sam <- sapply(strsplit(as.character(DF$Sam), "-"), function(x) 
       paste(x[1],sprintf("%03d", as.numeric(x[2])), sep="-"))
DF$Sam
#[1] "222-088" "537-457" "652-001" "787-892"

If we need a regex approach we can use gsubfn

library(gsubfn)
gsubfn("(\\d+)$", ~sprintf("%03d", as.numeric(x)), as.character(DF$Sam))
#[1] "222-088" "537-457" "652-001" "787-892"
like image 188
akrun Avatar answered Mar 02 '26 07:03

akrun


Another base option

Sam <- c("222-88", "537-457", "652-1", "787-892")
m <- gregexpr("[0-9]+$", Sam)
regmatches(Sam, m) <- sprintf('%03s', unlist(regmatches(Sam, m)))
Sam

# [1] "222-088" "537-457" "652-001" "787-892"
like image 37
rawr Avatar answered Mar 02 '26 06:03

rawr