Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import several rows from csv in R

Tags:

r

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.

like image 825
user785099 Avatar asked Feb 19 '23 05:02

user785099


1 Answers

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
like image 134
Blue Magister Avatar answered Mar 02 '23 20:03

Blue Magister