我有a CSV file (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
如何将整个文件放入我的R
会话中,如何解决或解决此EOF消息(这似乎更像是一个错误而非警告)?
我在阅读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
答案 0 :(得分:182)
您需要停用引用。
cit <- read.csv("citations.CSV", quote = "",
row.names = NULL,
stringsAsFactors = FALSE)
str(cit)
## 'data.frame': 112543 obs. of 13 variables:
## $ row.names : chr "10.2307/675394" "10.2307/30007362" "10.2307/4254931" "10.2307/20537934" ...
## $ id : chr "10.2307/675394\t" "10.2307/30007362\t" "10.2307/4254931\t" "10.2307/20537934\t" ...
## $ doi : chr "Archaeological Inference and Inductive Confirmation\t" "Sound and Sense in Cath Almaine\t" "Oak Galls Preserved by the Eruption of Mount Vesuvius in A.D. 79_ and Their Probable Use\t" "The Arts Four Thousand Years Ago\t" ...
## $ title : chr "Bruce D. Smith\t" "Tomás Ó Cathasaigh\t" "Hiram G. Larew\t" "\t" ...
## $ author : chr "American Anthropologist\t" "Ériu\t" "Economic Botany\t" "The Illustrated Magazine of Art\t" ...
## $ journaltitle : chr "79\t" "54\t" "41\t" "1\t" ...
## $ volume : chr "3\t" "\t" "1\t" "3\t" ...
## $ issue : chr "1977-09-01T00:00:00Z\t" "2004-01-01T00:00:00Z\t" "1987-01-01T00:00:00Z\t" "1853-01-01T00:00:00Z\t" ...
## $ pubdate : chr "pp. 598-617\t" "pp. 41-47\t" "pp. 33-40\t" "pp. 171-172\t" ...
## $ pagerange : chr "American Anthropological Association\tWiley\t" "Royal Irish Academy\t" "New York Botanical Garden Press\tSpringer\t" "\t" ...
## $ publisher : chr "fla\t" "fla\t" "fla\t" "fla\t" ...
## $ type : logi NA NA NA NA NA NA ...
## $ reviewed.work: logi NA NA NA NA NA NA ...
我认为是因为这种行(检查“Thorn”和“Minus”)
readLines("citations.CSV")[82]
[1] "10.2307/3642839,10.2307/3642839\t,\"Thorn\" and \"Minus\" in Hieroglyphic Luvian Orthography\t,H. Craig Melchert\t,Anatolian Studies\t,38\t,\t,1988-01-01T00:00:00Z\t,pp. 29-42\t,British Institute at Ankara\t,fla\t,\t,"
答案 1 :(得分:9)
我是一名新用户,我认为我会发布这个以防万一。我试图从包含一些西班牙字符的文本文件(用逗号分隔)中读取数据,我花了很长时间才弄明白。 我知道我需要使用UTF-8编码,将头文件arg设置为TRUE,并且我需要将sep arguemnt设置为“,”,但是我仍然会挂起。 After reading this post我尝试将填充arg设置为TRUE,但后来得到了相同的“引用字符串中的EOF”,我能够以与上面相同的方式修复它。我成功的read.table看起来像这样:
target <- read.table("target2.txt", fill=TRUE, header=TRUE, quote="", sep=",", encoding="UTF-8")
结果有西班牙语字符和我原来的相同颜色,所以我称之为成功!谢谢大家!
答案 2 :(得分:4)
在R帮助部分,如上所述,只需添加完全禁用引用:
quote = ""
到read.csv()为我工作。
错误“引用字符串中的EOF”出现在:
> iproscan.53A.neg = read.csv("interproscan.53A.neg.n.csv",
+ colClasses=c(pb.id = "character",
+ genLoc = "character",
+ icode = "character",
+ length = "character",
+ proteinDB = "character",
+ protein.id = "character",
+ prot.desc = "character",
+ start = "character",
+ end = "character",
+ evalue = "character",
+ tchar = "character",
+ date = "character",
+ ipro.id = "character",
+ prot.name = "character",
+ go.cat = "character",
+ reactome.id= "character"),
+ as.is=T,header=F)
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
EOF within quoted string
> dim(iproscan.53A.neg)
[1] 69383 16
读入的文件缺少6,619行。但是通过禁用引用
> iproscan.53A.neg = read.csv("interproscan.53A.neg.n.csv",
+ colClasses=c(pb.id = "character",
+ genLoc = "character",
+ icode = "character",
+ length = "character",
+ proteinDB = "character",
+ protein.id = "character",
+ prot.desc = "character",
+ start = "character",
+ end = "character",
+ evalue = "character",
+ tchar = "character",
+ date = "character",
+ ipro.id = "character",
+ prot.name = "character",
+ go.cat = "character",
+ reactome.id= "character"),
+ as.is=T,header=F,**quote=""**)
>
> dim(iproscan.53A.neg)
[1] 76002 16
没有错误地工作,所有行都被成功读入。
答案 3 :(得分:3)
实际上,使用read.csv()
来读取带有文本内容的文件并不是一个好主意,请禁用引号,因为集quote=""
只是一个临时解决方案,它只适用于单独的引号。还有其他原因会导致警告,例如某些特殊字符。
永久解决方案(使用read.csv()
),找出这些特殊字符是什么,并使用正则表达式来消除它们是一个想法。
您是否考虑过安装软件包{data.table}
并使用fread()
来读取文件。它速度更快,不会因为这个EOF警告而打扰你。请注意,它加载它的文件将存储为data.table对象,而不是data.frame对象。 class data.table有很多很好的功能,但无论如何,你可以根据需要使用as.data.frame()
对其进行转换。
答案 4 :(得分:2)
我也遇到了这个问题,并且能够使用以下方法解决类似的EOF错误:
read.table("....csv", sep=",", ...)
请注意,separator参数是在更通用的read.table()
。
答案 5 :(得分:1)
我遇到了类似的问题:EOF -warning,只有部分数据加载了read.csv()。我尝试了quotes =“”,但它只删除了EOF -warning。
但是看着没有加载的第一行,我发现其中一个单元格中有一个特殊字符,一个箭头→(十六进制值0x1A)。删除箭头后,我得到了正常加载的数据。
答案 6 :(得分:0)
我也有类似的问题。但在我的情况下,问题的原因是由于某些文本值中存在撇号(即单引号)。在处理包含法文文本的数据时,这尤其常见。 «欢乐之旅»。
因此,解决方案只是调整quote参数的默认设置以排除«'»符号,并因此使用 quote =“ \”“ (即双引号仅),一切正常。
我希望对您有所帮助。干杯。
答案 7 :(得分:0)
readr软件包将解决此问题。
install.packages('readr')
library(readr)
readr::read_csv('yourfile.csv')