0

I'm trying to export some data, however it contains blank values in some spaces. The data I imported (which is to be imported slightly modified, then exported) contains these blank, null values. After I export the file, these values are converted into blank strings. I want these values to stay as they were when I imported them.

They don't appear any different from a blank string in Excel, but when viewed in Notepad, the blanks appear as ,"", versus ,, as they do before being imported.

This appears to be exactly what I'm looking for, however, it doesn't produce the desired (or described) result.

I've tried using na = "" and na = NULL in both write.csv() and write.table(). Is there a way of accomplishing this? I'll include a few examples below of what I've tried, they all produce either NAs or ""s where I'd like empty values in my CSV.

(It's worth noting, I've included both NAs and ""s in this df, but I could easily standardize these values, so long as I can convert one to empty/null values in the CSV.)

df <- data.frame(c(1,NA,3,4), c(1,2,"",4))

write.table(df, "TestData.csv",
            na = "",
            row.names = FALSE,
            col.names = FALSE,
            append = FALSE,
            sep = ",")    
write.csv(df, "TestData.csv", na = character(0))
write.csv(df, "TestData.csv", na = "")
write.csv(df, "TestData.csv", na = NULL)
JStorey
  • 13
  • 1
  • 4
  • 1
    Your `write.table` option produced for me a file like you describe, with the NA replaced with and the "" carried through: first col = 1, , 3, 4 second col = "1", "2", "", "4" – Jon Spring Mar 08 '21 at 03:04
  • Not sure what was wrong before, but I reviewed that solution and you're right, it works fine! Thanks for pointing out my ignorance. Much appreciated! – JStorey Mar 08 '21 at 04:24

0 Answers0