Imagine you have a file
sink("example.txt")
data.frame(a = runif(10), b = runif(10), c = runif(10))
sink()
and would want to add some header information, like
/* created on 31.3.2011 */
/* author */
/* other redundant information */
How would I add this “header”? Doing it manually seems trivial. Hit a few Enters, copy/paste or write information and you’re done. Of course, in R, I could read in example.txt, create example2.txt, add header information and then example.txt.
I was wondering if there’s another way of appending files from the “top”. Other solutions (from c++ or Java…) also welcome (I’m curious how other languages approach this problem).
in R there is no need to work with an extra file. You can just do :
Yet, using the linux shell seems the most optimal solution, as R is not famous for performant file reading and writing. Especially not since you have to read in the complete file first.
Example :