useR
useR

Reputation: 3082

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

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

Upvotes: 10

Views: 13246

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269526

Try tihs:

data2 <- head(data2, -10)

Upvotes: 9

sgibb
sgibb

Reputation: 25736

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.

Upvotes: 12

Related Questions