Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a dataframe to csv file with value of NA as blank

Tags:

r

csv

write.table

There is a dataframe named cnbd, for example:

cnbd = data.frame(1,2,3,NA,NA,5) 

Thus the expression:

dim(cnbd)[1] 

give 1.

I want to write a dataframe like cnbd to a csv with:

write(file = filename, cnbd, append = TRUE) 

The problem comes:

  1. The values of csv file show cnbd with 6 rows not 1 row as 1,2,3,NA,NA,5.
  2. I need output cnbd show as 1,2,3,,,5 in csv file, no NAs.
like image 466
Lawes Avatar asked Jan 07 '14 10:01

Lawes


People also ask

How to write CSV from a list in R?

Use write. csv() to export R DataFrame to CSV file with fields separated by comma delimiter, header (column names), rows index, and values surrounded with double-quotes. You can also override this default behavior and export CSV without header, without row index or number, with no quotes e.t.c.

What function do we need to use to convert DataFrame to .CSV file?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.


1 Answers

Try this:

write.table(df, "cnbd.csv",             na = "",             row.names = FALSE,             col.names = FALSE,             append = TRUE,             sep = ",") 
like image 158
zx8754 Avatar answered Sep 23 '22 00:09

zx8754