13

I have a csv file which looks like this-

    #this is a dataset
    #this contains rows and columns

   ID     value1  value2   value3
   AA       5       6        5
   BB       8       2        9
   CC       3       5        2

I want read the csv file excluding those comment lines. It is possible to read mentioning that when it is '#' skip those line.But here the problem is there is an empty line after comments and also for my different csv file it can be various numbers of comment lines.But the main header will always start with "ID" from where i want to read the csv.

It is possible to specify somehow that when it is ID read from there? if yes then please give an example.

Thanks in advance!!

jessy
  • 191
  • 2
  • 8

3 Answers3

22

Use the comment.char option:

read.delim('filename', comment.char = '#')

Empty lines will be skipped automatically by default (blank.lines.skip = TRUE). You can also specify a fixed number of lines to skip via skip = number. However, it’s not possible to specify that it should start reading at a given line starting with 'ID' (but like I’ve said it’s not necessary here).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Hi, this - comment.char = '#' function worked very nicely to remove comments .thanks for that!! But still its showing that empty line at the beginning even if i use "blank.lines.skip = TRUE".any idea for that ?? – jessy Feb 10 '15 at 14:14
  • @jessy It seems like the lines aren’t really empty then. With the example text you provided, my code works, once I’ve stripped the spaces at the beginning of the lines (I’m assuming they’re not in your actual file, since otherwise the function doesn’t work at all). – Konrad Rudolph Feb 10 '15 at 14:50
2

For those looking for a tidyverse approach, this will make the job, similarly as in @Konrad Rudolph's answer:

readr::read_delim('filename', comment = '#')
elcortegano
  • 2,444
  • 11
  • 40
  • 58
1

If you know in advance the number of line beofre headers, you can use skip option (here 3 lines):

read.table("myfile.csv",skip=3, header=T)
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87