gpt4 book ai didi

r - 错误 : Text after processing all cols in fread (data. 表)

转载 作者:行者123 更新时间:2023-12-04 09:33:26 33 4
gpt4 key购买 nike

我尝试导入 text file在 R (3.4.0) 中,它实际上包含 4 列,但第 4 列大部分是空的,直到第 200,000+ 行。我使用包 data.table 中的 fread()(版本 1.10.4)

fread("test.txt",fill = TRUE, sep = "\t", quote = "", header = FALSE)

我收到此错误消息:

Error in fread("test.txt", fill = TRUE, sep = "\t", quote = "", header = FALSE) : 
Expecting 3 cols, but line 258088 contains text after processing all cols. Try again with fill=TRUE. Another reason could be that fread's logic in distinguishing one or more fields having embedded sep=' ' and/or (unescaped) '\n' characters within unbalanced unescaped quotes has failed. If quote='' doesn't help, please file an issue to figure out if the logic could be improved.

我检查了文件,在第 4 列(“8-4”)的第 258088 行中有其他文本。

然而,fill = TRUE 并没有像我预期的那样解决这个问题。我认为可能是 fread() 不恰本地确定了列号,因为附加列在文件中出现得很晚。所以我尝试了这个:

fread("test.txt", fill = TRUE, header = FALSE, sep = "\t", skip = 250000)

错误仍然存​​在。另一方面,

fread("test.txt", fill = TRUE, header = FALSE, sep = "\t", skip = 258080)

这没有错误。

我以为我找到了原因,但是当我用 dummy file 测试时,奇怪的事情发生了生成者:

write.table(matrix(c(1:990000), nrow = 330000), "test2.txt", sep = "\t", row.names = FALSE)

Excel 在第 250000 行的第 4 列中添加了“8-4”。当被 fread() 读取时:

fread("test2.txt", fill = TRUE, header = FALSE, sep = "\t")

它工作正常,没有错误消息,这应该表明一些延迟的附加列不一定会触发错误。

我也尝试过更改编码(“Latin-1”和“UTF-8”)或引用,但都没有帮助。

现在我感到一头雾水,但愿我已经用可重现的信息做足了功课。感谢您的帮助。

对于额外的环境信息,我的 sessionInfo() 是:

R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] dplyr_0.5.0 purrr_0.2.2.2 readr_1.1.1 tidyr_0.6.3
[5] tibble_1.3.3 ggplot2_2.2.1 tidyverse_1.1.1 stringr_1.2.0
[9] microbenchmark_1.4-2.1 data.table_1.10.4

loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 cellranger_1.1.0 compiler_3.4.0 plyr_1.8.4 forcats_0.2.0
[6] tools_3.4.0 jsonlite_1.5 lubridate_1.6.0 nlme_3.1-131 gtable_0.2.0
[11] lattice_0.20-35 rlang_0.1.1 psych_1.7.5 DBI_0.6-1 parallel_3.4.0
[16] haven_1.0.0 xml2_1.1.1 httr_1.2.1 hms_0.3 grid_3.4.0
[21] R6_2.2.1 readxl_1.0.0 foreign_0.8-68 reshape2_1.4.2 modelr_0.1.0
[26] magrittr_1.5 scales_0.4.1 rvest_0.3.2 assertthat_0.2.0 mnormt_1.5-5
[31] colorspace_1.3-2 stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3 broom_0.4.2

最佳答案

其实你提供的两个文件是有区别的,我想这就是fread输出不同的原因。

第一个文件在第 3 列之后有一个行尾,除了第 258088 行,其中第 4 列有一个制表符,然后是行尾。 (您可以使用选项“显示所有字符以确认”)。

另一方面,第二个文件在所有行中都有一个额外的选项卡,即一个新的空列。因此,在第一种情况下, fread 需要 3 列,然后找出第 4 列。相反,在第二个文件中,fread 需要 4 列。

我用 fill=TRUE 检查了 read.table,它适用于这两个文件。所以我认为 fread 的 fill 选项做了不同的事情。

我希望自 fill=TRUE 以来,所有的行都被用来推断列数(计算时间成本)。

在评论中有一些您可以使用的不错的解决方法。

关于r - 错误 : Text after processing all cols in fread (data. 表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44714323/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com