jessy
jessy

Reputation: 311

Skip comment line in csv file using R

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!!

Upvotes: 14

Views: 8966

Answers (3)

elcortegano
elcortegano

Reputation: 2684

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

readr::read_delim('filename', comment = '#')

Upvotes: 2

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

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)

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

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).

Upvotes: 23

Related Questions