跳过最后10行以读取csv文件(行数未知) [英] skip last 10 rows for read in csv file (unknown number of rows)

查看:124
本文介绍了跳过最后10行以读取csv文件(行数未知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道read.csv()

我知道如何跳过前10行,如下所示:

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

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

如何跳过最后10行?

我的方法是

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

还有其他方法吗?

谢谢

推荐答案

如@Thomas所建议的,您可以结合使用readLinesread.csv,例如:

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

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

但是我不确定这种方法是否比您的data2 <- data2[1:(dim(data2)[1]-10),]更好.如果您的后10行采用不同的格式并中断read.csv,则将是更好的方法.

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.

这篇关于跳过最后10行以读取csv文件(行数未知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆