Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "fread", how to eliminate comment line from csv?

I have to read a CSV file which has some comment lines(start with #) along with data rows. fread function is used to read this CSV file.

config <- fread("Configuration.csv")

CSV file snapshot

In this file number of lines are not fixed they might be changed. How to read the CSV without these comment lines.

Thanks in Advance!!!

like image 247
Bhavneet sharma Avatar asked Mar 05 '23 04:03

Bhavneet sharma


1 Answers

You can try to clean the data using grep before, not after:

config <- fread("grep -v '^#' Configuration.csv")

UPDATED:

If the aim of using fread() is to convert the data to data.table only, you can use read.table() instead with default comment.char="#" and then convert the result into data.table:

config <- as.data.table(read.table(header = TRUE, "Configuration.csv"))
like image 92
Grzegorz Sionkowski Avatar answered Mar 07 '23 01:03

Grzegorz Sionkowski