fread(data.table)选择列,如果未找到列,则抛出错误 [英] fread (data.table) select columns, throw error if column not found

查看:480
本文介绍了fread(data.table)选择列,如果未找到列,则抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用data.table的 fread 函数将csvfile加载到R中。它有一堆我不需要的列,所以 select 参数派上用场。我注意到,但是,如果select中指定的列之一不存在于csvfile中,fread将静默继续。如果csvfile中不存在所选择的列之一,是否有可能使R抛出错误?

I'm loading a csvfile into R with data.table's fread function. It has a bunch of columns that I don't need, so the select parameter comes in handy. I've noticed, however, that if one of the columns specified in the select does not exist in the csvfile, fread will silently continue. Is it possible to make R throw an error if one of the selected columns doesn't exist in the csvfile?

#csvfile has "col1" "col2" "col3" "col4" etc

colsToKeep <- c("col1", "col2" "missing")

data <- fread(csvfile, header=TRUE, select=colsToKeep, verbose=TRUE)



< data 将有两列: col1 col2 。剩余的列将按预期删除,但缺少将被忽略。这真的很高兴知道fread是跳过那个列,因为它没有找到它。

In the above example, data will have two columns: col1, col2. The remaining columns will be dropped as expected, but missing is silently skipped. It would certainly be nice to know that fread is skipping that column because it did not find it.

推荐答案

我建议解析先行第一行,然后抛出自己的错误。您可以:

I'd suggest parsing the first row pre-emptively, then throwing your own error. You could do:

read_cols <- function(file_name, colsToKeep) {
    header <- fread(file_name, nrows = 1, header = FALSE)
    all_in_header <- all(colsToKeep %chin% unlist(header))
    stopifnot(all_in_header)

    fread(file_name, header=TRUE, select=colsToKeep, verbose=TRUE)
}

my_data <- read_cols(csvfile, c("col1", "col2" "missing"))

这篇关于fread(data.table)选择列,如果未找到列,则抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆