Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip comment line in csv file using R

Tags:

r

csv

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

like image 900
jessy Avatar asked Feb 10 '15 13:02

jessy


People also ask

How do you skip the first line in R?

Sometimes there are a few lines of metadata at the top of the file. You can use skip = n to skip the first n lines; or use comment = "#" to drop all lines that start with (e.g.) # .

How do I read one column from a CSV file in R?

Method 1: Using read. table() function. In this method of only importing the selected columns of the CSV file data, the user needs to call the read. table() function, which is an in-built function of R programming language, and then passes the selected column in its arguments to import particular columns from the data.

How do I skip the first column of a CSV file in R?

"NULL" (note the quotes!) means skip the column, NA means that R chooses the appropriate data type for that column.

How do I add comments to a csv file?

Comments are supported in Outline Load utility input CSV files. For single line comments, place the hash character as the first character on the line; for example, # comment. Blank lines are ignored.


3 Answers

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

readr::read_delim('filename', comment = '#')
like image 54
elcortegano Avatar answered Nov 12 '22 22:11

elcortegano


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

like image 40
Konrad Rudolph Avatar answered Nov 12 '22 23:11

Konrad Rudolph


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)
like image 28
Colonel Beauvel Avatar answered Nov 12 '22 22:11

Colonel Beauvel