简体   繁体   English

如何在R中将二进制数据写入csv文件

[英]how to write binary data to csv file in R

I am trying to write binary data to a csv file for further reading this file with 'read.csv2', 'read.table' or 'fread' to get a dataframe. 我正在尝试将二进制数据写入csv文件,以进一步使用'read.csv2','read.table'或'fread'读取此文件以获得数据帧。 The script is as follows: 脚本如下:

library(iotools)
library(data.table)

#make a dataframe 
n<-data.frame(x=1:100000,y=rnorm(1:100000),z=rnorm(1:100000),w=c("1dfsfsfsf"))

#file name variable 
file_output<-"test.csv"

#check the existence of the file -> if true -> to remove it
if (file.exists(file_output)) file.remove(file_output)
#create a file
file(file_output, ifelse(FALSE, "ab", "wb"))

#to make a file object
zz <- file(file_output, "wb")
#to make a binary vector with column names
rnames<-as.output(rbind(colnames(n),""),sep=";",nsep="\t")
#to make a binary vector with dataframe
r = as.output(n, sep = ";",nsep="\t")

#write column names to the file
writeBin(rnames, zz)
#write data to the file
writeBin(r, zz)
#close file object
close(zz)

#test readings
check<-read.table(file_output,header = TRUE,sep=";",dec=".",stringsAsFactors = FALSE
                  ,blank.lines.skip=T)
str(check)
class(check)

check<-fread(file_output,dec=".",data.table = FALSE,stringsAsFactors = FALSE)
str(check)
class(check)

check<-read.csv2(file_output,dec=".")
str(check)
class(check)

The output from the file is attached: 该文件的输出被附加:

在此处输入图片说明

My questions are : 我的问题是

  1. how to remove the blank line from the file without downloading to R? 如何从文件中删除空白行而不下载到R?
    It has been made on purpose to paste a binary vector of colnames as a dataframe. 故意将colnames的二进制矢量粘贴为数据帧。 Otherwise colnames were written as one-column vector. 否则,将同名写为一栏向量。 Maybe it is possible to remove a blank line before 'writeBin()'? 也许可以在“ writeBin()”之前删除空白行?

  2. How make the file to be written all numeric values as numeric but not as a character? 如何使文件将所有数字值都写为数字而不是字符?

I use the binary data transfer on purpose because it is much faster then 'write.csv2'. 我故意使用二进制数据传输,因为它比“ write.csv2”要快得多。 For instance, if you apply 例如,如果您申请

system.time(write.table.raw(n,"test.csv",sep=";",col.names=TRUE))

the time elapsed will be ~4 times as less as 'write.table' used. 经过的时间将比使用的“ write.table”少约4倍。

I could not comment on your question because of my reputation but I hope it helps you. 由于我的声誉,我无法评论您的问题,但希望对您有所帮助。

Two things come in my mind 我想到两件事
  1. Using the fill in read.table in which, if TRUE then in that case the rows have unequal length, blank fields are implicitly added. 使用read.table中的fill (如果为TRUE则在这种情况下,行的长度不相等)将隐式添加空白字段。 (do ??read.table ) (做??read.table

  2. You have mentioned blank.lines.skip=TRUE . 您已经提到blank.lines.skip=TRUE If TRUE blank lines in the input are ignored. 如果为TRUE ,则输入中的空白行将被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM