how to read the last line of a txt file in R

246 views Asked by At

I want to read the last line of a txt file by readLines in R, how can I achieve this?

con<-file("test.txt","r")
line<-readLines(con,n=-1)
cat(line,"\n")
close(con)

the text file is as followed

3B1CHLH_PI210.PV       07-MAY-15 10:33:52.5       173.944       Good
3B1CHLH_PI210.PV       07-MAY-15 10:34:29.0       173.379       Good
3B1CHLH_PI210.PV       07-MAY-15 10:35:25.6       172.890       Good
3B1CHLH_PI210.PV       07-MAY-15 10:35:58.3       173.009       Good
3B1CHLH_PI210.PV       07-MAY-15 10:36:50.9       173.677       Good
3B1CHLH_PI210.PV       07-MAY-15 10:37:18.6       173.069       Good
3B1CHLH_PI210.PV       07-MAY-15 10:37:49.2       173.408       Good
3B1CHLH_PI210.PV       07-MAY-15 10:38:39.6       173.445       Good
3B1CHLH_PI210.PV       07-MAY-15 10:39:12.5       173.445       Good

I can only get all the lines, but I only want to read the last line, is there any way in R like the fseek in C

0

There are 0 answers