Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skip last 10 rows for read in csv file (unknown number of rows)

Tags:

r

csv

i knew that there is options skip for read.csv()

I knew how to do for skip first 10 rows as follow:

data2<-read.csv("book4.csv", skip=10, header=T)

How about for skipping last 10 rows?

My approach is

data2 <- data2[1:(dim(data2)[1]-10),]

Is there any alternative way?

Thanks

like image 826
useR Avatar asked Apr 15 '14 10:04

useR


2 Answers

As @Thomas suggested you can combine readLines and read.csv, e.g.:

df <- read.csv(text=paste0(head(readLines("file.csv"), -10), collapse="\n"))

But I am not sure that this approach is better than your data2 <- data2[1:(dim(data2)[1]-10),]. It would be the better approach if your last 10 lines are in a different format and break read.csv.

like image 69
sgibb Avatar answered Sep 22 '22 02:09

sgibb


Try tihs:

data2 <- head(data2, -10)
like image 33
G. Grothendieck Avatar answered Sep 18 '22 02:09

G. Grothendieck