12

Hi I've a huge file and i want to import only the last 100 rows from that file. How can we do that using read.csv() or any alternative?

Prasun Velayudhan
  • 537
  • 1
  • 7
  • 19
  • If you're concerned about speed, then try `fread` from "data.table" and then just extract the rows you need. Similarly, you can use `sqldf`. If you're on a Unix system, you have access to the `tail` command that could be useful. – A5C1D2H2I1M1N2O1R2T1 Aug 30 '13 at 06:37
  • 1
    I know we can use os specific commands but i'm looking for a work around in R itself! – Prasun Velayudhan Aug 30 '13 at 07:36

7 Answers7

23

The package R.utils has a function called countLines(). You could do:

l2keep <- 10
nL <- countLines("your.csv")
df <- read.csv("your.csv", header=FALSE, skip=nL-l2keep)
lauratboyer
  • 334
  • 1
  • 4
3

If you are on a *nix system, you are better off using the tail -n 100 command to take the last 100 rows. Anything implemented in R would be slower and potentially much slower is your file is truly huge.

If you are using Windows, you may want to take a look at this SO question.

Community
  • 1
  • 1
ktdrv
  • 3,602
  • 3
  • 30
  • 45
  • ya that's true. So what you are telling is that using some windows function to get the last 100 rows put it in a file and then import into R? – Prasun Velayudhan Aug 30 '13 at 07:27
  • Pretty much. You can do `seek()` and other "fancy" stuff in R but good luck finding something that is as fast or as simple. – ktdrv Aug 30 '13 at 16:36
2

Improvement on @lauratboyer's answer if you want to include headers too:

# read headers only
column_names <- as.vector(t(read.csv("your.csv", header=FALSE, colClasses='character', nrows=1)))

# then last n lines
l2keep <- 10
nL <- R.utils::countLines("your.csv")
df <- read.csv("your.csv", header=FALSE, col.names=column_names, skip=nL-l2keep)
balping
  • 7,518
  • 3
  • 21
  • 35
1

You could use the nrows and skip arguments in read.csv. E.g. if you have a file with 10000 rows and you would only like to import the last 100 rows you could try this:

read.csv("yourfile.csv",nrows=100,skip=9900)

But if it is speed you want, you're probably better off with the solutions given by @Ananda Mahto and @ktdrv

Rob
  • 1,460
  • 2
  • 16
  • 23
0

The quick and dirty way that works for me - use fread to read large files while setting select = 1 so that only the first column is read. Then use fread again to read data from the desired rows. Fread is much faster than read.csv or other similar variants. More on fread vs read.csv here: Reason behind speed of fread in data.table package in R

Gautam
  • 2,597
  • 1
  • 28
  • 51
0

Read file, use tail function a<-read.csv('c:/..') tail(a,100L)

h612
  • 544
  • 2
  • 11
-2

give appropriate skip parameter in read.csv()

B K
  • 723
  • 8
  • 17
  • 1
    this doesn't answer the OP's question. They want to *read from a file* only the last 100 rows. Your answer assumes the data set has already been read. – Ben Bolker Jun 10 '15 at 16:55
  • This reply is useless. It's just a bad copy of @Rob's reply (who replied first). For the record, I am not criticizing Rob's answer, but only this B K's reply – Bruce_Warrior May 08 '23 at 04:58