Skip to navigation

Reading recalcitrant csv files in R (line n did not have m elements)

Trying to read.csv() or read.table() a csv file in R caused some headaches. In particular, I got:

line 14 did not have 98 elements

It turns out in this instance it was some (unqouted) string entries containing a double-quoted word. The solution was:

read.table("file.csv",quote="", ...)

Recently, I had the same problem due to inconsistent line endings, and needed:

read.table("file.csv",fill=TRUE, ...)

… which pads shorter lines as necessary.

So, those two are worth checking out, as might be setting …

read.table("file.csv", comment.char="")

if your file contains # symbols that are not intended as comment prefixes. This is default for read.csv() anyway.

One Response to “Reading recalcitrant csv files in R (line n did not have m elements)”

  1. Nilaksha Says:

    You saved my life big time. :D
    Invaluable piece of information.