4

我有 N 行的 csv 文件,其中每一行对应一个数据点。有没有办法从这个 csv 文件中读取一组预先指定的行。

4

2 回答 2

4

It is faster to extract the rows you want with another tool like Python or the Unix shell, but if R is your best choice:

file.in <- file('yourfile.txt', 'rt')
##file.header <- readLines(file.in,n=1) #do this if you are skipping a header
##change this to the lines that you want - line numbers must not decrease
ind.to.read <- c(10,15,20) 
##this is how many lines you want to skip in between lines you want
ind.to.skip <- c(ind.to.read[1],diff(ind.to.read)) - 1
# [1] 9 4 4 
##returns a list of data frames corresponding to rows
lines.to.keep <- lapply(ind.to.skip, function(x) { 
  readLines(file.in,n=x)
  read.table(file.in,nrow=1) 
}) 
small.df <- do.call(rbind,lines.to.keep) #put the data frames together
于 2012-12-11T22:55:28.417 回答
4

您可以将skipandnrows参数指定为read.csv

skip整数:开始读取数据之前要跳过的数据文件的行数。

nrowsinteger:要读入的最大行数。忽略负数和其他无效值。

于 2012-12-11T21:09:25.943 回答