R-code for reading in the last n lines of a csv file
Monday, October 29th, 2007In R there are several ways to read a data file. There are ways to skip the first k lines…and only read n lines, but there seems to be no easy way to read the last n lines of a file. I faced this problem will doing some work this weekend and found that the following solution which invokes some UNIX features can be used.
file.name = "yourfile.csv" #.txt can work too
file.rows.wc = system(paste('wc -l ', filename, sep=""), intern=T)
file.rows = as.numeric(system('cut -d" " -f1', input=file.rows.wc, intern=T))n = 30 # number of rows from the end we want
data = read.csv(file=file.name, header=T, skip=(file.rows-n), nrows=n)
Using this approach can save time and memory especially if the data to be read is large, and the data to be used is small.
