7

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.

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?

Given a data frame:

DF <- as.data.frame(matrix(seq(20),nrow=5,ncol=4))
colnames(DF) <- c("A", "B", "C", "D")
write.table(DF, "DF.csv", row.names=FALSE, sep=",")

I can append a new data frame to the last line like this

A <- 1
A <- data.frame(A)
A$B <- 1
A$C <- 1
A$D <- 1
write.table(A, "DF.csv", row.names=FALSE, sep=",", append=TRUE, col.names=FALSE)

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

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

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

Vinterwoo
  • 3,843
  • 6
  • 36
  • 55
  • In any computer programming language, you can generally assume that text files are fixed and cannot be changed. Think about it like this: a text file is really just one long continuous string of characters. The lines we see are a text editor interpreting one of the characters('\n') in a certain way. What you are asking is if it's possible to insert some characters into that string. How would you do that without overwriting the characters that are already there? If you wanted to replace 3 characters with 3 other characters, you could just overwrite the original 3 characters. – 7stud Apr 21 '13 at 04:35
  • You should search SO for post regarding inserting lines in data.frames – IRTFM Apr 21 '13 at 04:55
  • I came across "R: Insert a vector as a row in data.frame" previously, but my issue has been trying to write/ append a row into a separate .csv file outside of R. Not a data frame within R – Vinterwoo Apr 21 '13 at 05:15
  • 2
    I asked something similar a while ago, and the answer was, not out of the box. http://stackoverflow.com/questions/5500522/how-to-prepend-to-a-file-add-at-the-top – Roman Luštrik Apr 21 '13 at 05:58

2 Answers2

5

Write your own function:

my.write.table <- function(df, filename, sep)
{
   ## read the existing content
   temp.df <- read.table(filename, sep)

   ## append in front
   df <- rbind(df, temp.df)

   ## write back the whole data frame
   write.table(df, filename, sep)
}
Nishanth
  • 6,932
  • 5
  • 26
  • 38
1

I came across the same problem. My solution is to use readLines.

contents=readLines(filename)
newcontents=c(firstline,contents)
writeLines(newcontents, filename)
SBMVNO
  • 582
  • 3
  • 13