r - 如何跳过阅读readr中的某些列

标签 r readr

这个问题在这里已经有了答案:





Only read selected columns

(5 个回答)


3年前关闭。




我有一个名为“test.csv”的简单 csv 文件,其内容如下:

colA,colB,colC
1,"x",12
2,"y",34
3,"z",56

假设我想跳过 colA 中的阅读,而只是在 colB 和 colC 中阅读。我想要一个通用的方法来做到这一点,因为我有很多文件要读取,有时 colA 被称为其他东西,但 colB 和 colC 总是相同的。

根据 read_csv 文档,完成此操作的一种方法是传递 col_types 的命名列表,并仅命名要保留的列:
read_csv('test.csv', col_types = list(colB = col_character(), colC = col_numeric()))

通过不提及 colA 它应该从输出中删除。但是,生成的数据框是:
Source: local data frame [3 x 3]

      colA colB colC
    1    1    x   12
    2    2    y   34
    3    3    z   56

我做错了什么还是 read_csv 文档不正确?根据帮助文件:

If a list, it must contain one "collector" for each column. If you only want to read a subset of the columns, you can use a named list (where the names give the column names). If a column is not mentioned by name, it will not be included in the output.

最佳答案

那里有一个答案,我只是没有足够努力地搜索:
https://github.com/hadley/readr/issues/132

显然,这是一个已更正的文档问题。最终可能会添加此功能,但 Hadley 认为能够仅更新一种列类型而不删除其他列类型更有用。

更新:已添加功能

以下代码来自 readr documentation :
read_csv("iris.csv", col_types = cols_only( Species = col_factor(c("setosa", "versicolor", "virginica"))))
这将仅读取 iris 数据集的 Species 列。为了仅读取特定列,您还必须通过列规范,即 col_factor , col_double , 等等...

关于r - 如何跳过阅读readr中的某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31150351/

相关文章:

r - `read_fwf` 和 `vroom_fwf` 意外地跳过第一行?

R - 对于 view_html() 我如何将限制更改为超过 100

r - 使用 Readr read_csv 函数抑制所有消息/警告

r - dplyr : how to read a tsv file with headers while skipping some lines?

在 R 中删除国家 map 边界之外的数据

r - 从 R 实现的 promise 中获取值(value)

r - 大矩阵中的子集慢

r - 如何使用 R 将干涉图傅里叶变换为红外光谱?

r - 单击后折叠仪表板侧边栏菜单

readr::type_convert 弄乱了时间列