How to remove double quotes around NULL values in write.csv?
I am working on exporting a data.frame to a csv for use in an ecommerce system after I have done some analysis on it.
I am removing the NA values before the extract as they are not allowed in the system I am adding 开发者_JS百科the data to. The process I have looks like this, my data.frame is called prod_out:
prod_out[is.na(prod_out)] <- c("")
prod_con<-file('product_output.csv',encoding="utf8")
write.csv(prod_out,file=prod_con,append=FALSE,eol="\r",quote=TRUE,row.names=FALSE)
This generates the file, however, for the fields that are NULL they are all double quoted like this:
...,"",...
I need to not have the double quotes for NULL fields and leave them for any character field like this:
...,,...
I did change quote=FALSE, however that removed all double quoting and I need the character fields to remain intact. Is there any way to unquote the NULL values?
Any help is appreciated.
Thanks,
Jason
try this:
df<-data.frame(w=c("a","b"),x=runif(2),y=rep(NA,2),z=runif(2))
write.csv(df,na="",quote=TRUE,row.names=FALSE)
First, it's helpful to know, if you don't already that "NA" and NA are not the same thing:
> x = c(3, 4, 5, 7, 12, "NA", "NA", 12, 43)
> x
[1] "3" "4" "5" "7" "12" "NA" "NA" "12" "43"
> is.na(x)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> # now convert each "NA" to NA
> x[x=="NA"] = NA
> x
[1] "3" "4" "5" "7" "12" NA NA "12" "43"
> is.na(x)
[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
In sum, to "remove the double quotes" from each NA, you need to convert "NA" to NA, as above.
source.write
.option("nullValue", null)
.option("emptyValue", null)
.csv(csvFilePath)
Try this, it worked for me to handle null values in scala.
精彩评论