1

I have a csv file with a blank row above the column names.
Is it possible to correct for this in R?

Right now, I open the csv and remove the blank row before running the R code. But I want to avoid this work if I can.

What is the correct/best way to remove the blank row and then label the columns.

I have tried:

 df <- read.csv("SourceFile/filename.csv", header= TRUE, row.names = 2)

but I get this error:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names

SRWRay
  • 11
  • 3
  • `read.csv` should have a `skip` option – akash87 May 16 '19 at 21:09
  • 1
    If the row is totally blank it will be skipped automatically. `txt <- "\n\none,two\n1,2"; read.csv(text=txt, header=TRUE)` - `blank.lines.skip = TRUE` is the default. I suspect something else is wrong here, possibly a quoting character in a field. Try setting `quote=""` as an argument too. – thelatemail May 16 '19 at 22:50

2 Answers2

1

In the readr package, the read_csv function has a skip argument. https://readr.tidyverse.org/reference/read_delim.html

There is also a skip_empty_rows argument if you want to be more aggressive.

cardinal40
  • 1,245
  • 1
  • 9
  • 11
0

For your case, try:

read.csv(...,skip=1)

Hope this is helpful!

Bensstats
  • 988
  • 5
  • 17