Reputation: 427
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")
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!!!
Upvotes: 4
Views: 5457
Reputation: 529
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"))
Upvotes: 5
Reputation: 27732
I assume you are using windows... if so, you can feed the results from findstr to fread()
.
The data from the orinal answer is saved to test.csv
.
data.table::fread( cmd = 'findstr "^[^#]" test.csv', sep = "\n", header = FALSE )
# V1
#1: line,with,data
#2: line2,with,data
findstr /b /v test.csv
gives the same result
When on linux, you can (probably) pass the output from a grep
command to fread()
.
This sulution is not very helpful when dealing with only one file, but is will help when reading in multiple files using lapply()
text = "line,with,data
#commentline,with,data
line2,with,data"
data.table::fread( text, sep = "\n", header = FALSE )[!grepl( "^#", V1 )]
Upvotes: 2