【发布时间】:2016-09-24 13:38:25
【问题描述】:
我目前正在使用包readr 读取文件。这个想法是使用read_delim 逐行读取,以找到我的非结构化数据文件中的最大列。代码输出存在parsing 问题。我知道这些并将在导入后处理列类型。有没有办法关闭problems(),因为通常的options(warn) 不起作用
i=1
max_col <- 0
options(warn = -1)
while(i != "stop")
{
n_col<- ncol(read_delim("file.txt", n_max = 1, skip = i, delim="\t"))
if(n_col > max_col) {
max_col <- n_col
print(max_col)
}
i <- i+1
if(n_col==0) i<-"stop"
}
options(warn = 0)
我试图抑制的控制台输出如下:
.See problems(...) for more details.
Warning: 11 parsing failures.
row col expected actual
1 1####4 valid date 1###8
【问题讨论】:
-
我觉得你应该在导入之前从 R 中修复文件。也许 awk 更适合这项任务?
-
suppressWarnings(x <- readr::parse_integer(c("1X", "blah", "3")))似乎工作...... -
最好打开
file连接,一次读取文件一行并计算分隔符?max(sapply(readLines("file.txt"),function(x){length(strsplit(x,"\t")[[1]])}))