[R] parsing numeric values
    baptiste auguie 
    baptiste.auguie at googlemail.com
       
    Wed Nov 18 12:57:06 CET 2009
    
    
  
Dear list,
I'm seeking advice to extract some numeric values from a log file
created by an external program. Consider the following example,
input <-
readLines(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"))
## this is what I want
results <- c(as.numeric(strsplit(grep("<ax>", input,val=T), " ")[[1]][8]),
             as.numeric(strsplit(grep("<ay>", input,val=T), " ")[[1]][8]),
             as.numeric(strsplit(grep("<aax>", input,val=T), " ")[[1]][9]),
             as.numeric(strsplit(grep("<aay>", input,val=T), " ")[[1]][9])
             )
## [1] 0.00137700 0.00019412 0.00137700 0.00019412
The use of strsplit is not ideal here as there is a different number
of space characters in the lines containing <ax> and <aax> for
instance (hence the indices 8 and 9 respectively).
I tried to use gsubfn for a cleaner construct,
strapply(input, "<ax> += +([0-9.]+)", c, simplify=rbind,combine=as.numeric)
but I can't seem to find the correct regular expression to deal with
the exponent.
Any tips are welcome!
Best regards,
baptiste
    
    
More information about the R-help
mailing list