title: how to write binary data to csv file in R
body: <p>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:  </p>
  
  <pre><code>library(iotools)
  library(data.table)
  
  #make a dataframe 
  n&lt;-data.frame(x=1:100000,y=rnorm(1:100000),z=rnorm(1:100000),w=c("1dfsfsfsf"))
  
  #file name variable 
  file_output&lt;-"test.csv"
  
  #check the existence of the file -&gt; if true -&gt; 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 &lt;- file(file_output, "wb")
  #to make a binary vector with column names
  rnames&lt;-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&lt;-read.table(file_output,header = TRUE,sep=";",dec=".",stringsAsFactors = FALSE
                    ,blank.lines.skip=T)
  str(check)
  class(check)
  
  check&lt;-fread(file_output,dec=".",data.table = FALSE,stringsAsFactors = FALSE)
  str(check)
  class(check)
  
  check&lt;-read.csv2(file_output,dec=".")
  str(check)
  class(check)
  </code></pre>
  
  <p>The output from the file is attached:</p>
  
  <p><a href="https://i.stack.imgur.com/wBF3v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wBF3v.png" alt="enter image description here"></a></p>
  
  <p><strong><em>My questions are</em></strong>: </p>
  
  <ol>
  <li><p>how to remove the blank line from the file without downloading to R?<br>
  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()'?</p></li>
  <li><p>How make the file to be written all numeric values as numeric but not as a character?</p></li>
  </ol>
  
  <p>I use the binary data transfer on purpose because it is much faster then 'write.csv2'. For instance, if you apply</p>
  
  <pre><code>system.time(write.table.raw(n,"test.csv",sep=";",col.names=TRUE))
  </code></pre>
  
  <p>the time elapsed will be ~4 times as less as 'write.table' used.</p>