I have csv file with N rows, where each row corresponds to a data point. Is there a way to just read a set of pre-specified rows from this csv file.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With