The Script below is splitting Item code;
Example
MR32456
into MR324, MR325, MR326
.
MR3091011
into MR309, MR301, MR300, MR301, MR301
How should i amend the script so that for MR3091011
, it will split into MR309, MR310, MR311
?
rule2 <- c("MR")
df_1 <- test[grep(paste("^",rule2,sep="",collapse = "|"),test$Name.y),]
SpaceName_1 <- function(s){
num <- str_extract(s,"[0-9]+")
if(nchar(num) >3){
former <- substring(s, 1, 4)
latter <- strsplit(substring(s,5,nchar(s)),"")
latter <- unlist(latter)
return(paste(former,latter,sep = "",collapse = ","))
}
else{
return (s)
}
}
df_1$Name.y <- sapply(df_1$Name.y, SpaceName_1)