How to import last 100 rows using read.csv() in R

2019-04-05 17:55发布

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?

标签: r csv import
7条回答
三岁会撩人
2楼-- · 2019-04-05 18:07

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

查看更多
放我归山
3楼-- · 2019-04-05 18:08

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.

查看更多
【Aperson】
4楼-- · 2019-04-05 18:13

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

查看更多
男人必须洒脱
5楼-- · 2019-04-05 18:13

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

查看更多
Explosion°爆炸
6楼-- · 2019-04-05 18:19

give appropriate skip parameter in read.csv()

查看更多
贪生不怕死
7楼-- · 2019-04-05 18:23

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)
查看更多
登录 后发表回答