Anthony Martin
Anthony Martin

Reputation: 787

fread is not reading the columns names properly

I am trying to use a csv generated from the Apple mobility reports, which can be found here.

Now everything works relatively fine, and I am able to get the .csv as intended, which looks something like this text:

csvtxt <- "geo_type,region,2020-01-14,2020-01-15,2020-01-16
country/region,Albania,50.1,100.2,75.3"

But when I fread it, the first line, which is unsurprisingly a column name line, is not recognized as so, even with the option check.names = FALSE that I found somewhere here but cannot find again.

library(data.table)
fread(csvtxt, check.names = FALSE)
#               V1      V2         V3         V4         V5
#1:       geo_type  region 2020-01-14 2020-01-15 2020-01-16
#2: country/region Albania       50.1      100.2       75.3

Is there a way to get this data to import so that the column name line is recognized properly?

Upvotes: 2

Views: 1876

Answers (1)

zx8754
zx8754

Reputation: 56259

We need to force the header by setting it to TRUE.

library(data.table) # R version 4.0.2, data.table_1.13.2

fread(csvtxt, header = TRUE)
#          geo_type  region 2020-01-14 2020-01-15 2020-01-16
# 1: country/region Albania       50.1      100.2       75.3

From the manuals:

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.

Confusion might be from read.csv where header is TRUE by default:

read.csv(text = csvtxt)
#         geo_type  region X2020.01.14 X2020.01.15 X2020.01.16
# 1 country/region Albania        50.1       100.2        75.3

Upvotes: 3

Related Questions