Thank you - worked. -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] Sent: Wednesday, January 28, 2009 6:14 AM To: Yannis Tzamouranis Cc: r-sig-gui@stat.math.ethz.ch Subject: Re: [R-gui] Splitting lines of a csv file without affecting substrings in double quotes
Try: out <- read.csv(con, nrows = 1, header = FALSE, as.is = TRUE) out <- paste(out, collapse = "\t") On Wed, Jan 28, 2009 at 5:49 AM, Yannis Tzamouranis <yannis_tzamoura...@yahoo.com> wrote: > 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 > No virus found in this incoming message. Checked by AVG - http://www.avg.com 6:37 AM _______________________________________________ R-SIG-GUI mailing list R-SIG-GUI@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-gui