【发布时间】:2021-10-06 18:13:43
【问题描述】:
我正在尝试使用 read_delim() 读取文件并选择列的子集(长期运行)以定义为特定类型。
例如,我有一个包含 6 列的文件。我想选择第 1 列(“名称”)作为字符,然后选择第 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 列,我想选择列的子集/运行而不手动写出它们。
我试过了:
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') ="
我确信这很简单,但我已经花了好几个小时试图解决它!
【问题讨论】: