Nina Tan
Nina Tan

Reputation: 119

String Splitting in R

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)

Upvotes: 1

Views: 80

Answers (2)

Sandipan Dey
Sandipan Dey

Reputation: 23101

Try this:

str <- 'MR3091011'
paste(substring(str,1,4), strsplit(str,"")[[1]][-(1:4)], sep='')

[1] "MR309" "MR301" "MR300" "MR301" "MR301"

for a list of strings you can try:

strlst <- c("MR32456", "MR3091011")
lapply(strlst, function(str) paste(substring(str,1,4), 
                                   strsplit(str,"")[[1]][-(1:4)], sep=''))    
[[1]]
[1] "MR324" "MR325" "MR326"

[[2]]
[1] "MR309" "MR301" "MR300" "MR301" "MR301"

[EDIT]

groups <- unlist(strsplit(sub('([[:alpha:]]+)(\\d)(\\d{2})(\\d{2})(\\d{2})', '\\1 \\2 \\3 \\4 \\5', 'MR3091011'), split=' '))
paste0(groups[1], groups[2], groups[3:5])
# [1] "MR309" "MR310" "MR311"

Upvotes: 0

Sotos
Sotos

Reputation: 51582

Borrowing a split function from this post, and vectorizing it, we can do the following,

fun1 <- function(x){ 
  sapply(seq(from=1, to=nchar(substr(x, 4, nchar(x))), by=2), function(i) substr(substr(x, 4, nchar(x)), i, i+1))
} 

fun1 <- Vectorize(fun1)

Map(paste0, substr(x, 1, 3), fun1(x))

#$MR3
#[1] "MR309" "MR310" "MR311"

#$MR3
#[1] "MR324" "MR356"

Upvotes: 2

Related Questions