将数据帧写入csv文件,将NA值设置为空 [英] Write a dataframe to csv file with value of NA as blank

查看:491
本文介绍了将数据帧写入csv文件,将NA值设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个 dataframe 命名为 cnbd ,例如:

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

因此表达式:

  dim(cnbd)[1] 



给1。



code> cnbd 添加到csv中:

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

问题出现了:


  1. csv文件的值显示 cnbd ,6行不是1行作为 1,2,3, NA,5

  2. 我需要输出 cnbd 显示为 1,2 ,3 ,,, 5 in csv file,no NAs。


解决方案

尝试以下操作:

  write.table(df,cnbd.csv,
na = ,
row.names = FALSE,
col.names = FALSE,
append = TRUE,
sep =,)


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.

解决方案

Try this:

write.table(df, "cnbd.csv",
            na = "",
            row.names = FALSE,
            col.names = FALSE,
            append = TRUE,
            sep = ",")

这篇关于将数据帧写入csv文件,将NA值设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆