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!!
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.) # .
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.
"NULL" (note the quotes!) means skip the column, NA means that R chooses the appropriate data type for that column.
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.
For those looking for a tidyverse
approach, this will make the job, similarly as in @Konrad Rudolph's answer:
readr::read_delim('filename', comment = '#')
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With