tags:

views:

67

answers:

2

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 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

+2  A: 

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)
kohske
And make sure you skip the step where you replace NA with c("")
Brandon Bertelsen
Exactly, thanks.
kohske
I see what you mean. Thank you @kohske and @Brandon for the information and the education on how the NA values work and the best way to handle them. I appreciate your help.
analyticsPierce
+1  A: 

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.

doug
@doug thank you for the example. this is very helpful to understand how the NA values are different.
analyticsPierce