Hello,
 
My question pertains to splitting a string which contains a substring
enclosed in double-quotes.  The substring should not be split by strsplit.
The whole point is to read in a csv file just like Excel would do on its
own.
So I have a csv file that looks like this (small snippet):
Trial,Parameter,0,0.5,1.5,2.5
1,ESG.Economies.CNY.NominalYieldCurve.CashTotalReturnIndex,0.98,1,1.0367,1.0
66
1,"ESG.Economies.CNY.NominalSpotRate(Govt, 1)",0.025,0.0367,0.0281,0.0215
1,"ESG.Economies.CNY.NominalSpotRate(Govt, 2)",0.029,0.0392,0.0294,0.0227
1,"ESG.Economies.CNY.NominalSpotRate(Swap, 3)",0.037,0.0405,0.029,0.0238
1,"ESG.Economies.CNY.NominalSpotRate(Govt, 4)",0.036,0.0414,0.0304,0.02
1,"ESG.Economies.CNY.NominalSpotRate(Swap, 5)",0.038,0.042,0.031,0.026
...
 
I read it in with readLines, then try to split it with strsplit.
 
The desired result is this (which is how Excel reads it in automatically).
Notice that what is in the double quotes should remain unaffected by the
splitting:
Trial   Parameter 0    0.5     1.5     2.5
1       ESG.Economies.CNY.NominalYieldCurve.CashTotalReturnIndex     0.98
1       1.0367        1.066
1       ESG.Economies.CNY.NominalSpotRate(Govt, 1)    0.025   0.0367  0.0281
0.0215
1       ESG.Economies.CNY.NominalSpotRate(Govt, 2)    0.029   0.0392  0.0294
0.0227
1       ESG.Economies.CNY.NominalSpotRate(Govt, 3)    0.037   0.0405  0.029
0.0238
1       ESG.Economies.CNY.NominalSpotRate(Govt, 4)    0.036   0.0414  0.0304
0.02
1       ESG.Economies.CNY.NominalSpotRate(Govt, 5)    0.038   0.042   0.031
0.026
 
My efforts went like this:
bridgeCSV <- "myfile.csv";              
con.B <- file(description=bridgeCSV, open="rt", blocking = FALSE)
bridgeheader <- readLines(con.B, n=1)
the.bridge <- readLines(con.B, n=-1) 
the.bridge.1 <- strsplit(the.bridge, ",")
the.bridge.2 <- strsplit(unlist(the.bridge.1), "\"")
 
I tried a few ways along these lines but did not manage to get the text in
caps as a single field.
For example:
> the.bridge.1 <- strsplit(the.bridge[2], ",")
> the.bridge.1
[[1]]
[1] "1"
"\"ESG.Economies.CNY.NominalSpotRate(Govt" " 1)\""

[4] "0.025"                                    "0.0367"
"0.0281"                                  
[7] "0.0215"                                  
 
Maybe I am missing something obvious? Does anyone have an idea how I can
successfully parse this to look as the desired result above?
 
Yannis

        [[alternative HTML version deleted]]

_______________________________________________
R-SIG-GUI mailing list
R-SIG-GUI@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-gui

Reply via email to