Skip to content
Prev 200704 / 398503 Next

parsing numeric values

Hi,

Thanks for the alternative approach. However, I should have made my
example more complete in that other lines may also have numeric
values, which I'm not interested in. Below is an updated problem, with
my current solution,

tc <- textConnection(
"some text
 <ax> =    1.3770E-03     <bx> =    3.4644E-07
 <ay> =    1.9412E-04     <by> =    4.8840E-08

other text
 <aax>  =    1.3770E-03     <bbx> =    3.4644E-07
 <aay>  =    1.9412E-04     <bby> =    4.8840E-08

lots of other material,  including numeric values
 1.23E-4 123E5 12.3E-4 123E5 123E-4 123E5
 12.3E-4 123E5 12.3E-4 123E5 123E-4 123E5
etc...")

input <-
readLines(tc)
close(tc)

## I want to retrieve the values for
## <ax>, <ay>, <aax> and <aay> only

results <- c(
strapply(input, "<ax> += +(\\d+\\.\\d+E[-+]?\\d+)", as.numeric,
simplify = rbind),
strapply(input, "<ay> += +(\\d+\\.\\d+E[-+]?\\d+)", as.numeric,
simplify = rbind),
strapply(input, "<aax> += +(\\d+\\.\\d+E[-+]?\\d+)", as.numeric,
simplify = rbind),
strapply(input, "<aay> += +(\\d+\\.\\d+E[-+]?\\d+)", as.numeric,
simplify = rbind))

results

Using the suggested base R solution, I've come up with this variation,

z <- gsub("[^[:digit:]E.+-]"," ", grep("<ax>|<ay>|<aax>|<aay>", input,
value=TRUE))

test <- scan(textConnection(z),what=0)
test[seq(1, length(test), by=2)]


Thanks again,

baptiste

2009/11/18 Bert Gunter <gunter.berton at gene.com>: