Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fread reads empty csv wrongly when there is a numeric colname

If I have an empty csv file with the headers like this –

❯ cat an.csv                                                                                                                                                                                                              10:36:25
a,b,c,d

In Rconsole when I fread it I get an empty DT.


❯ b = fread('an.csv')

❯ b
Empty data.table (0 rows and 4 cols): a,b,c,d

But if I have a numeric header name in one of the cols, fread reads it as –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

❯ cat another.csv                                                                                                                                                                                                         10:38:10
a,b,3

❯ bb = fread('another.csv')

❯ bb
   V1 V2 V3
1:  a  b  3

Why does fread read it wrongly when there are numeric header names. How do I fix it ? It should be an empty dt reagardless of what the col names are.

Edit:

In addition to the answer, the docs also explain how fread distinguishes between considering the first line as a header line or not –

  header: Does the first data line contain column names? Defaults
          according to whether every non-empty field on the first data
          line is type character. If so, or TRUE is supplied, any empty
          column names are given a default name.

>Solution :

Use:

fread('another.csv', header = TRUE)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading