在 R 中使用 read_csv 和 col_select 和 id 参数出现异常错误

Unusual error using read_csv and col_select and id argument in R

当我尝试将 id 参数 read_csvcol_select() 一起使用时,我遇到了一个奇怪的错误,但前提是我使用它们在 非连续方式(例如c(1:2,4))。

特别是当您使用 readr::read_csv 从文件路径导入多个 csv 文件并使用 id 参数时 使用 col_select 参数通过以非连续方式按 position 引用列,则会出现错误。

但是,如果我们删除 id 参数,按位置引用列而没有“间隙”,甚至按名称引用列,那么它将起作用。

有办法解决这个问题吗?或者这是一个错误?

请看下面:

#pretend this file file path has at least two csv files in it
files <- "insert_file_path"

#this will not work, notice there is a "gap" in the refernece (no column 3)
##the error suggest it can't subset columns that don't exist

read_csv(files,
         id = "source",
         col_select = c(1:2,4),#notice that I do not pull in the 3rd column
         )

#this will work now that I references without any "gaps"

read_csv(files,
         id = "source",
         col_select = c(1:3,4) #however it will work when I pull in columns in a continuous way
         )

#howewever if we remove the "id" argument, the below code will work (even though there is a "gap")

read_csv(files,
         #id = "source",
         col_select = c(1:2,4),#however this will work (pulls in different columns though)
         )

我已将以下内容作为 issue 提交到 readr 上,它已被标记为错误。

https://github.com/tidyverse/readr/issues/1395