1

I have data that looks like the following:

Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741

It's a huge file with 100k observations. I read it in R and trim it down to 10k and then write it to csv. However, I couldn't find how to unquote only the header without losing the quotes within the data. If I keep quote = TRUE, this is what the final data looks like but I want to get rid of the quotes in the header.

"Scenario","ScenName","Step","Date","GBP_fx","EUR_fx"
0,"assets",0,"30/09/2016",1,0.865126741

Any suggestions?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Katherine
  • 111
  • 4
  • 1
    As [this post](http://stackoverflow.com/questions/42032990/controlling-number-of-decimal-places-in-write-table-while-maintaining-class-num/42033117#42033117) indicates, you can control quotes by column, but I am not sure there is a way to control them for the header. – lmo Mar 29 '17 at 18:58
  • You could try to read first row of your data file i.e. column names with quotes. Then replace quotes with blank spaces and use unquoted column names as a new column names to your file. – Aleksandr Mar 29 '17 at 19:18

3 Answers3

5

You can write the header then append the rows, e.g. :

# input sample
testDF <- read.csv(text=
'Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741')

# custom write.csv function
write.csv.noheaderquote <- function(x,file){
  # write header
  write.table(head(x,0),file=file,sep=',',quote=FALSE,row.names = FALSE)
  # append the rest of the rows
  write.table(x,file=file,sep=',',row.names=FALSE,append=TRUE,col.names=FALSE)
}

# use the new function to write your csv
write.csv.noheaderquote(testDF,"destfile.csv")

Resulting destfile.csv :

Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741
digEmAll
  • 56,430
  • 9
  • 115
  • 140
0

I'm not sure everything is necessary, but if your input is saved in csv "mycsv.csv", and you are okay including stringr

library(stringr)
df = read.table("mycsv.csv", sep = ",", quote = "", header = TRUE)
colnames(df) = str_replace( str_replace(colnames(df), "X.", ""), "\\.", "" )
Miha
  • 2,559
  • 2
  • 19
  • 34
rsmith54
  • 818
  • 9
  • 15
0

It looks the parameter quote itself will do it. use quote="" as in:

testDF <- read.csv(text=
'Scenario,ScenName,Step,Date,GBP_fx,EUR_fx
0,"assets",0,"30/09/2016",1,0.865126741', quote="")

one gets:

> testDF
  Scenario ScenName Step         Date GBP_fx    EUR_fx
1        0 "assets"    0 "30/09/2016"      1 0.8651267
IBrum
  • 345
  • 1
  • 9