Home > Software design >  fread reads empty csv wrongly when there is a numeric colname
fread reads empty csv wrongly when there is a numeric colname

Time:02-01

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 -

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

CodePudding user response:

Use:

fread('another.csv', header = TRUE)
  •  Tags:  
  • Related