6

How can we read specific rows into R using the read.csv command? Say if I have a CSV file which has 10 rows of data, and I just want to only read the 5th row of data, I tried doing the following but it doesn't seem to be working:

myFile <- "MockData.csv"
myData <- read.csv(myFile, row.names=5)
myData

Thanks!

bartektartanus
  • 12,415
  • 4
  • 70
  • 92
Outrigger
  • 139
  • 1
  • 3
  • 10
  • possible duplicate of [Read csv from specific row](http://stackoverflow.com/questions/6592219/read-csv-from-specific-row) and [In R, how can read lines by number from a large file?](http://stackoverflow.com/questions/7156770/in-r-how-can-read-lines-by-number-from-a-large-file/7156792#7156792) – Thomas Jul 02 '13 at 09:20
  • 1
    Thanks for the feedback. I have actually looked at that post prior to posting this. However, it does not seem like there is a straightforward solution even though the idea seems simple enough, hence I wanted to clarify further. – Outrigger Jul 02 '13 at 09:36
  • Well, that's why you also have my answer. – Thomas Jul 02 '13 at 09:37

2 Answers2

16

Try:

myData <- read.csv(myFile, nrows=1, skip=4)
Thomas
  • 40,508
  • 11
  • 98
  • 131
2

In order to read from 2nd row till the bottom of the file row by row (assuming that the first row is the header and can be omitted), what has been done is as such:

myFile <- "MockData.csv"
myData <- read.csv(myFile, skip=0, nrows=1)

And then:

myData <- read.csv(myFile, skip=1, nrows=1)

Followed by:

myData <- read.csv(myFile, skip=2, nrows=1)

And so on...

Outrigger
  • 139
  • 1
  • 3
  • 10