我有一个CSV文件(24.1 MB),下载链接在此,但我无法完全将其读入我的R会话。当我在电子表格程序中打开文件时,我可以看到112,544行。但是,当我使用read.csv
将其读入R时,我只得到了56,952行,并出现了警告:
cit <- read.csv("citations.CSV", row.names = NULL,
comment.char = "", header = TRUE,
stringsAsFactors = FALSE,
colClasses= "character", encoding= "utf-8")
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
EOF within quoted string
我可以使用readLines
将整个文件读入R中:
rl <- readLines(file("citations.CSV", encoding = "utf-8"))
length(rl)
[1] 112545
但是我无法将这个数据以表格形式(通过 read.csv
)重新导入R:
write.table(rl, "rl.txt", quote = FALSE, row.names = FALSE)
rl_in <- read.csv("rl.txt", skip = 1, row.names = NULL)
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
EOF within quoted string
如何解决或绕过这个EOF消息(看起来更像是一个错误而不是警告),以便将整个文件导入我的R
会话?
我在使用其他读取CSV文件的方法时也遇到了类似的问题:
require(sqldf)
cit_sql <- read.csv.sql("citations.CSV", sql = "select * from file")
require(data.table)
cit_dt <- fread("citations.CSV")
require(ff)
cit_ff <- read.csv.ffdf(file="citations.CSV")
这是我的sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] tools tcltk stats graphics grDevices utils datasets methods base
other attached packages:
[1] ff_2.2-11 bit_1.1-10 data.table_1.8.8 sqldf_0.4-6.4
[5] RSQLite.extfuns_0.0.1 RSQLite_0.11.4 chron_2.3-43 gsubfn_0.6-5
[9] proto_0.3-10 DBI_0.2-7
fread
起作用怎么样?我更喜欢它,因为它比read.csv
快得多。但是fread
似乎不接受quote
参数... - Benfread
通常不支持嵌套引号,但我相信很快就会有解决方法出现。https://dev59.com/3HHYa4cB1Zd3GeqPHyBa - dickoawrite.csv()
时,我有7,000行数据,但使用read.csv()
只返回了403行。添加quote = ""
后,我得到了410行。即使使用read.table()
也没有改善。我想知道还有什么其他的尝试方法... - Hack-R