[R] options other than regex

Evan Cooch ev@n@cooch @ending from gm@il@com
Fri May 25 15:43:19 CEST 2018


Hi --

I'm looking for alternatives to regex for a fairly simply 'reformatting' 
problem. Alternatives only because a lot of folks have trouble 
parsing/interpreting regex expressions, and I'm looking for suggestions 
for something more 'transparent'.

Here is an example of what I'm trying to do. Take the following string, 
which I call x, and for each character in the string, replace that 
character with the character, followed by a decimal. The following big 
of regex works...and illustrates the reformatting I'm after:

x <- '10110111'
print(x)

y <- sub("\\s+$", "", gsub('(.{1})', '\\1.', x))
print(y)

I had a look at formatC or prettyNum as another way to get there from 
here, but couldn't get it to work:

x <- '10110111'
hold <- prettyNum(as.numeric(x), big.mark = ".", big.interval = 1,
         format = "d", flag = "0", width = nchar(x))
print(hold)


I tried making big.mark a decimal, but that fails, since it confuses 
'prettyNum'. OK, so I try a 2-step approach

x <- '10110111'
hold <- prettyNum(as.numeric(x), big.mark = "x", big.interval = 1,
         format = "d", flag = "00", width = nchar(x))
hold2 <- (gsub("x",".",hold))
print(hold2)

Seems to work, but...doesn't work (at least, based on what I tried) if 
the first character(s) in the string are 0's. [Whereas the regex 
approach handles this just fine...]


x <- '0010110111'
hold <- prettyNum(as.numeric(x), big.mark = "x", big.interval = 1,
         format = "d", flag = "00", width = nchar(x))
hold2 <- (gsub("x",".",hold))
print(hold2)


Basically, it strips off the leading 0's. I'm sure I'm missing something 
with prettyNum/formatC, but I'm also guessing there are alternatives. 
Suggestions?

Thanks in advance.



More information about the R-help mailing list