Question

I want to insert a space : for example x<-"cyclox 100500 tab" ,y<-" glipicon mg 700600 tab". I want the output as " cyclox 100 500 tab" and "glipicon mg 700 600 tab" ie gap between "100500" to "100 500" and "700600" to "700 600"

Now what I am doing is finding where the 6 digits are available. for that I have done

f<-regexec("[0-9]{6}",x)[[1]][1]  

by applying to x i am getting f=8 and for y f=13

for converting the data I am using

 x<-sub( '(?<=.{10})', ' ', x, perl=TRUE )
 y<-sub( '(?<=.{15})', ' ', y, perl=TRUE )

as I want space at 8+2 position and 13+2 position and thus getting the desired output

But when I am using t<-f+2 and so f becomes 10 in the first case and doing:

x<-sub( '(?<=.{t})', ' ', x, perl=TRUE )

nothing is happening. I need to apply this to many data so I need to use a variable like t.

Can anyone please tell me how to do this

Thanks

Was it helpful?

Solution

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 )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top