fread 没有正确读取列名

fread is not reading the columns names properly

我正在尝试使用从 Apple 移动报告生成的 csv,可以找到 here

现在一切正常,我能够按预期获得 .csv,它看起来像这样的文本:

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

但是当我 fread 它时,第一行,不出所料是列名行,没有被识别为这样,即使使用我在这里某处找到但找不到的选项 check.names = FALSE再次.

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

有没有办法导入这些数据,以便正确识别列名行?

我们需要通过将其设置为 TRUE 来强制 header。

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

来自手册:

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.

混淆可能来自 read.csv,其中 header 默认为真:

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