1

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. 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:

enter image description here

My questions are:

  1. how to remove the blank line from the file without downloading to R?
    It has been made on purpose to paste a binary vector of colnames as a dataframe. Otherwise colnames were written as one-column vector. Maybe it is possible to remove a blank line before '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'. 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.

1 Answer 1

1

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. (do ??read.table)

  2. You have mentioned blank.lines.skip=TRUE. If TRUE blank lines in the input are ignored.

Sign up to request clarification or add additional context in comments.

1 Comment

Look at my section 'test reading ' - i have such an option. but it has no desired result. at least at my workstation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.