Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scan EOF error while reading CSV file

Tags:

r

csv

I am trying to read a CSV file into R. I tried:

data <- read.csv(file="train.csv")
Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string

But, this reads in only a small percentage of the total observations. Then I tried removing quotes:

 data <- read.csv(file="train.csv",quote = "",sep = ",",header = TRUE)
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  more columns than column names

Since the data is text, it seems there is some issue with the delimiter.

It is difficult to share the entire data set as it is huge. I tried going to the line where the error comes, but there seems to be no non printable character. I also tried other readers like fread(), but to no avail.

like image 594
Zephyr Avatar asked May 28 '15 04:05

Zephyr


People also ask

What is EOF in CSV?

DAT data, there must be an EOF character (EOF = End Of File). Most editors do this automatically and these characters are essential.

What is quote in read CSV?

For read. csv(), the default quote parameter is quote="\"", which means that only double quotes will be used to delimit strings, not single quotes. Because two of your sample names had apostrophes (single quotes), the read. table() function tried to include everything between those two as a single string.

How do I read a csv file into R?

The CSV file to be read should be either present in the current working directory or the directory should be set accordingly using the setwd(…) command in R. The CSV file can also be read from a URL using read. csv() function.


1 Answers

Have encountered this before. Can be very tricky. Try a specialized CSV reader.:

library(readr)
data <- read_csv(file="train.csv")

This should do it.

like image 174
NEO Avatar answered Oct 30 '22 14:10

NEO