select 在 readr 中使用 col_type() 时的列子集

select subset of columns when using col_type() in readr

我正在尝试读取包含 read_delim() 和 select 列子集(长 运行)的文件以定义为特定类型。

例如,我有一个包含 6 列的文件。我想 select 第 1 列 ('name') 作为字符,然后 select 第 2-6 列作为整数。我可以通过手动指定列名来做到这一点:

df <- read_delim(file = "data.txt", col_type = list(name = col_character(), id_1 = col_integer(), id_2 = col_integer(), id_3 = col_integer(), id_4 = col_integer(), id_5 = col_integer()), delim = " ")

但是我的数据有 100 多列,我想 select 一个 subset/run 列而不用手动写出来。

我试过:

df <- read_delim(file = "data.txt", col_type = list(name = col_character(), id_1:id_5 = col_integer()), delim = " ")

df <- read_delim(file = "data.txt", col_type = list(name = col_character(), select('id_1':'id_5') = col_integer()), delim = " ")

但是我得到一个错误:

Error: unexpected '=' in:
"col_type = list(name = col_character(), select('id_1':'id_5') ="

我确信这很简单,但我花了很多时间试图解决它!

一个选项是传递一个命名的 listsetNames

df <- read_delim(file = "data.txt", 
     col_type = setNames( c(list(col_character()),  
           rep(list(col_integer()), 5)),
             c("name", paste0("id_", 1:5))), delim = " ")