Read.csv() throws error
Asked Answered
R

8

8

I have been trying to read the excel file but seems like there is something wrong. The file is stored in Documents folder in excel format.

These are the error messages that I get :

table <- read.csv(file.choose(),header=T,sep='\t')

Warning messages:
1: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  line 1 appears to contain embedded nulls
2: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  incomplete final line found by readTableHeader on 

also, since these were warnings , I happened to ignore them. But nothing has been read into "table" either:

table
# [1] PK...
# <0 rows> (or 0-length row.names)
Reconnaissance answered 16/9, 2014 at 15:35 Comment(2)
Related: #24735411Caribbean
and also: #23209964 including highlighting the skipNul = TRUE, optionCaribbean
J
11

read.csv doesn't read XLS(X) files, only CSV files. Try opening your Excel file in Excel, exporting it to CSV and reissuing your read.csv command (depending on your system language, you might want to use read.csv2 instead).

Jerkwater answered 16/9, 2014 at 15:39 Comment(2)
The error message has his command: table<-read.csv(file.choose(),header=T,sep='\t')Babbitt
@ydaetskcoR, you're right, I got confused with the file.choose() command (which is some really cool function I've just learned about). Editing.Jerkwater
F
9

I had a similar error, e.g:

A <- read.csv("tel.csv", sep = ",")
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  empty beginning of file
In addition: Warning messages:
1: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  invalid input found on input connection 'tel.csv'
2: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  line 1 appears to contain embedded nulls
3: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  incomplete final line found by readTableHeader on 'tel.csv'

For solution I tried:

A <- read.csv("tel.csv", sep = ",",
              fileEncoding="utf-16")

It worked.

Fanciful answered 29/5, 2016 at 19:21 Comment(2)
This turned out to be a file encoding issue for me also. see: here and hereWithhold
Indeed, I agree.Fanciful
F
4

If you're trying to read in an xlsx file, use the xlsx library, or export them as csv files. read.table or read.csv will not work for Excel files.

install.packages("xlsx")
library(xlsx)
table <- read.xlsx("file.xlsx", 1)
Fuji answered 16/9, 2014 at 15:43 Comment(0)
B
1

First of all, check that your CSV is in fact a CSV rather than an Excel file (you hint that that might be the case in your question). read.csv reads in delimited text files and can't handle Excel files (either .xls or .xlsx).

If it is a delimited text file then looking at the error message looks like your CSV (well, tab separated values file) might have some empty column names which read.csv() is unable to handle.

The second warning also then thinks that the last row of your file is incomplete which may be caused by whatever is outputting the file to combine separators when some of the fields are empty.

They're warnings because they don't stop the program or exit it but it's saying that things might not be as you expect.

Babbitt answered 16/9, 2014 at 15:39 Comment(0)
S
1

By definition a .csv file has comma separated values; in your R code you use tab ("\t") as the delimiter. If you have an actual csv file, you should be able to enter:

csvfile<-read.csv("csvfilename.csv")

or alternatively

csvfile<-read.table("csvfilename.csv",sep=",")

though really the first command should suffice. It's strange that you have to specificy a tab to delimit columns in a csv file, unless excel did something wonky to your data table.

You could always use the xlsx package in R and then write the file to the format you want as well.

Supererogate answered 18/9, 2014 at 4:22 Comment(1)
Csv tends to just imply a delimited text file. Most csv parsers will allow a choice of delimiter and r's read.csv is no exception.Babbitt
R
1

I was facing a similar issue but you can try using the following code using the skipNul argument:

read.table("filename.csv", sep = "\t", 
   col.names = c("A", "B", "C"), 
   na.strings = NA, skipNul = TRUE, header = TRUE)

The main thing to be noticed here is the skipNul argument. In this case, I have used the col.names because I noticed that in this case there was a problem in identifying the column names correctly.

Let me know if you are still not able to get it correctly.

Keep Coding!

Roundhouse answered 16/8, 2021 at 16:41 Comment(0)
C
0

The file you are trying to read could be a UTF-16 encoded file as if it were not so encoded. Try with the fileEncoding = "UTF-16" argument

Catabasis answered 31/3, 2015 at 6:56 Comment(0)
C
0

One possible cause is accidentally trying to load an alias to the file instead of the file itself. In R for Mac, trying to use read.csv on an alias gives the error you got.

Centrifuge answered 14/12, 2015 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.