Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to append to the first line of an exisiting file?
    primarykey
    data
    text
    <p>I've looked at several of the functions that are able to add text to an existing data file (.csv or .txt) such as write.table, write.lines, or sink.</p> <p>When the append argument =TRUE, the new data is always added after the last existing line of the file. Is it possible to add data to an existing file on first line (below a header)- AKA opposite of append?</p> <p>Given a data frame:</p> <pre><code>DF &lt;- as.data.frame(matrix(seq(20),nrow=5,ncol=4)) colnames(DF) &lt;- c("A", "B", "C", "D") write.table(DF, "DF.csv", row.names=FALSE, sep=",") </code></pre> <p>I can append a new data frame to the last line like this</p> <pre><code>A &lt;- 1 A &lt;- data.frame(A) A$B &lt;- 1 A$C &lt;- 1 A$D &lt;- 1 write.table(A, "DF.csv", row.names=FALSE, sep=",", append=TRUE, col.names=FALSE) </code></pre> <p>Which is close to what I want. But I would really like to have the above line to be added to the first line of the DF.csv (right below the header) like so</p> <pre><code>A B C D 1 1 1 1 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20 </code></pre> <p>To be clear, I'd not looking to add a row into a data frame within R. I am hoping to add a row to the beginning of a file outside of the R environment. Just as append can be used to add data to the end of an external .csv file, I am hoping to "append" data to the beginning of a .csv file, so that my latest data always comes up in the first row (to avoid scrolling through to the end of a long file to see the most current data). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload