Yusuf Bello
Yusuf Bello

Reputation: 1

Error Message when Opening a CSV File in R

I attempted opening a csv file in R using R studio but got this warning message:

In readLines("persons.csv") : incomplete final line found on 'persons.csv'

Please what is wrong with the file, and how can I fix it?

Upvotes: 0

Views: 723

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269431

You can likely ignore this as it probably still worked. Here is an example without a final newline which gives that warning and another one which has the final newline which does not give the warning. Both worked.

cat("a,b\n1,2", file = "test1.csv")
read.csv("test1.csv")
##   a b
## 1 1 2
## Warning message:
## In read.table(file = file, header = header, sep = sep, quote = quote,  :
##   incomplete final line found by readTableHeader on 'test1.csv'

cat("a,b\n1,2\n", file = "test2.csv")
read.csv("test2.csv")
##   a b
## 1 1 2

To address this try one of these:

  1. Just ignore it as it probably worked.

  2. Bring the file into a text editor and write it out again. That often eliminates the warning.

  3. Use readr::read_csv. The indicated argument eliminates many messages that are otherwise output by that command.

     library(readr)
     read_csv("test1.csv", show_col_types = FALSE)
     ## # A tibble: 1 x 2
     ##       a     b
     ##   <dbl> <dbl>
     ## 1     1     2
    
  4. Use data.table::fread. It won't give that message.

     library(data.table)
     fread("test1.csv", data.table = FALSE)
     ##   a b
     ## 1 1 2
    
  5. From the Windows cmd line use this (note dot)

     echo. >> test1.csv
    

    or under bash (no dot)

     echo >> test.csv
    

Upvotes: 2

Related Questions