0

I have a csv file with fixed headers. But some of the column values can be missing with empty commas. This is creating problems for read.csv.sql. Am I missing any parameter for this function? I expect to read null/NA - is there any workaround?

sample file content -

day,car1,car2

1,bmw,audi

2,merc,bmw

3,,audi

4,,

I get this error -

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 45 elements

Community
  • 1
  • 1
Nishanth
  • 6,932
  • 5
  • 26
  • 38
  • I cannot reproduce that error with your example; the following code works as expected without error: ```library(sqldf); tmp <-tempfile(); write("day,car1,car2 \n 1,bmw,audi \n 2,merc,bmw \n 3,,audi \n 4,,", file = tmp); read.csv.sql(tmp); unlink(tmp)``` – orizon Dec 05 '12 at 06:19
  • I think I deleted my comment by mistake: I had asked to see the read.csv command & the options you had used. – A_K Dec 05 '12 at 06:25
  • Thanks. After close examination I found that the number of commas were not matching! It works now. – Nishanth Dec 05 '12 at 06:25
  • I was having difficulty because the original file was ~500MB – Nishanth Dec 05 '12 at 06:26

1 Answers1

0

The huge file was first split into smaller chunks. The csv was then opened with Excel with , as de-limiter. There was a mis-match in number of commas in header(45 elements) and the next line(43 elements).

Nishanth
  • 6,932
  • 5
  • 26
  • 38