Hopefully I understand this question correctly (if Darkness returns...).
NA
values in R should not pose any problem when writing to a CSV file since there is an argument in write.table
, "na
", described as the string to use for missing values in the data. Thus, your NA
values can be replaced with nothing when writing your data.frame
to a file.
Here's a small example:
Create an unbalanced data.frame
Notice the empty "300" column and the incomplete "400" column. In R, these are populated with NA
values.
df <- data.frame(`100` = c(1,2,3,4,5), `200` = c(1,2,3,4,5),
`300` = NA, `400` = c(letters[1:3], NA, NA),
check.names=FALSE)
df
# 100 200 300 400
# 1 1 1 NA a
# 2 2 2 NA b
# 3 3 3 NA c
# 4 4 4 NA <NA>
# 5 5 5 NA <NA>
Write the data to a CSV file
Here, I'm writing to a tempfile in the R workspace and viewing it with cat(readLines(...))
for convenience and demonstration. In actuality, you would probably be writing this to a CSV in your working directory and opening it with another program. Note the correct formation of the CSV file according to the input, but with nothing where an NA
was in the original data.
x <- tempfile()
write.csv(df, file = x, na = "", row.names = FALSE)
cat(readLines(x), sep="\n")
# "100","200","300","400"
# 1,1,,"a"
# 2,2,,"b"
# 3,3,,"c"
# 4,4,,
# 5,5,,