regex - Grab data from strings in R using regular expression -
now string looks like:
"interest.usd,vol=[integrated,(0,0.101),(0.2,0.108),(1,0.110),(2,0.106), (3,0.102),(4,0.09),(5,0.091),(6,0.09128272)],drift=[integrated,(0.002,0.09), (0.24,0.0007),(0.4,0.007),(1,-0.033),(2,-0.005),(3,-0.0041), (4,-0.3505),(5,-0.65),(7,-0.08346),(8,-0.049),(9,-0.0613),(10,-0.019)], risk_neutral=yes,lambda=0.09,fx_volatility=0.01,fx_correlation=0.9" i want grab info next "vol" , "drift" in matrix format like:
vol matrix:
0,0.101 0.2,0.108 1,0.110 2,0.106 3,0.102 4,0.09 5,0.091 6,0.09128272 and single value 0.09 lambda. guess shuold utilize regular expression, not familiar that. suggestion? :)
p.s. tried using:
str_extract_all(text,'[ .+? ]') try info bewteen [ , ], returns "."
here's way extract values in r. let's assume strings posted stored in variable named a. in order create things easier, i'm going utilize helper function: getcapturedmatches(). can
expr <- "(vol|drift)=\\[integrated,([^\\]]*)\\]" mm <- regcapturedmatches(a,gregexpr(expr,a, perl=t))[[1]] expr <- "\\(([^,]+),([^,]+)\\)" vv <- regcapturedmatches(mm[,2],gregexpr(expr,mm[,2], perl=t)) first pass extract vol , drift elements in mm , split comma delimited lists vv. can combine info 1 big data.frame
tt <- map(data.frame, col=mm[,1], val=lapply(vv, function(x) {class(x)<-"numeric"; x})) dd<-do.call(rbind, unname(tt)) in end dd like
col val.1 val.2 1 vol 0.000 0.10100000 2 vol 0.200 0.10800000 3 vol 1.000 0.11000000 4 vol 2.000 0.10600000 5 vol 3.000 0.10200000 6 vol 4.000 0.09000000 7 vol 5.000 0.09100000 8 vol 6.000 0.09128272 9 drift 0.002 0.09000000 10 drift 0.240 0.00070000 11 drift 0.400 0.00700000 12 drift 1.000 -0.03300000 13 drift 2.000 -0.00500000 14 drift 3.000 -0.00410000 15 drift 4.000 -0.35050000 16 drift 5.000 -0.65000000 17 drift 7.000 -0.08346000 18 drift 8.000 -0.04900000 19 drift 9.000 -0.06130000 20 drift 10.000 -0.01900000 this method allows number of repeated values in each of sections.
if did want simple matrices then
map(function(a,b) {class(b)<-"numeric"; b}, mm[,1], lapply(vv, function(x) {class(x)<-"numeric"; x})) will give named list of matrices.
regex r
No comments:
Post a Comment