You could try something this:
sapply(strsplit(c(x, y), '(?<=0)(?=[1-9])', perl=TRUE), paste, collapse=' ')
# [1] "cyclox 100 500 tab" " glipicon mg 700 600 tab"
This assumes that your split will always be between a 0 and nonzero digit. If this assumption doesn't apply to your larger dataset, you could change the pattern to (?<=\\d{3})
, to split after three digits.
That being said, the reason your attempt to t
to specify the count isn't working is because you're using t
inside a string literal, and you can't directly reference variables inside strings. You can, however, substitute variables into strings. Take a look at the sprintf
function.
x<-sub(sprintf("(?<=.{%d})", t), ' ', x, perl=TRUE )