This is not a valid CSV, so you have to do your own parsing. But assuming the convention is this, you can simply switch using scan to take advantage of most of your abilities:
- If the field begins with a quote, it is quoted.
- If the field does not start with a quote, it is raw
next_field<-function(stream) { p<-seek(stream) d<-readChar(stream,1) seek(stream,p) if(d=="\"") field<-scan(stream,"",1,sep=",",quote="\"",blank=FALSE) else field<-scan(stream,"",1,sep=",",quote="",blank=FALSE) return(field) }
Assuming the above convention, this is sufficient for parsing as follows:
s<-file("example.csv",open="rt") header<-readLines(s,1) header<-scan(what="",text=header,sep=",") line<-replicate(length(header),next_field(s)) setNames(as.data.frame(lapply(line,type.convert)),header)
Type ID NAME CONTENT RESPONSE GRADE SOURCE
1 A 3 NA I have comma, ha! I have open double quotes "A NA
However, in practice, you may need to first write the fields, quoting them, to another file so that you can simply read.csv in the corrected format.
A. Webb
source share