I would use scan
for this if you know how many lines the log has:
scan("foo.txt",sep="\n",what="char(0)",skip=100)
If you donβt know how much you need to skip, you have no choice but to go to
- reading in everything and taking the last n lines (if possible),
- using
scan("foo.txt",sep="\n",what=list(NULL))
to find out how many entries there are, or - using some algorithm to go through the file, saving only the last n lines each time
The last option might look like this:
ReadLastLines <- function(x,n,...){ con <- file(x) open(con) out <- scan(con,n,what="char(0)",sep="\n",quiet=TRUE,...) while(TRUE){ tmp <- scan(con,1,what="char(0)",sep="\n",quiet=TRUE) if(length(tmp)==0) {close(con) ; break } out <- c(out[-1],tmp) } out }
allowing:
ReadLastLines("foo.txt",100)
or
ReadLastLines("foo.txt",100,skip=1e+7)
if you know that you have over 10 million rows. This can save reading time when you start to have very large magazines.
EDIT: Actually, I didn't even use R for this, given the size of your file. On Unix, you can use the tail command. There is a version of Windows for this, somewhere in the toolbox. I have not tried to do this yet.