我目前正在使用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
suppressWarnings(x <- readr::parse_integer(c("1X", "blah", "3")))
是可以工作的... - coryfile
连接,逐行读取文件并计算分隔符的数量?max(sapply(readLines("file.txt"),function(x){length(strsplit(x,"\t")[[1]])}))
- Spacedman