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)
4

8 回答 8

11

read.csv不读取 XLS(X) 文件,只读取 CSV 文件。尝试在 Excel 中打开您的 Excel 文件,将其导出为 CSV 并重新发出您的read.csv命令(取决于您的系统语言,您可能希望使用它read.csv2)。

于 2014-09-16T15:39:44.593 回答
9

我有一个类似的错误,例如:

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'

对于我尝试的解决方案:

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

有效。

于 2016-05-29T19:21:01.690 回答
4

如果您尝试读取 xlsx 文件,请使用 xlsx 库,或将它们导出为 csv 文件。read.table 或 read.csv 不适用于 Excel 文件。

install.packages("xlsx")
library(xlsx)
table <- read.xlsx("file.xlsx", 1)
于 2014-09-16T15:43:51.703 回答
1

根据定义, .csv 文件具有逗号分隔值;在您的 R 代码中,您使用制表符 ("\t") 作为分隔符。如果你有一个实际的 csv 文件,你应该能够输入:

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

或者

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

虽然真的第一个命令就足够了。奇怪的是,您必须指定一个制表符来分隔 csv 文件中的列,除非 excel 对您的数据表做了一些不稳定的事情。

你总是可以在 R 中使用 xlsx 包,然后将文件写入你想要的格式。

于 2014-09-18T04:22:52.093 回答
1

首先,检查您的 CSV 实际上是 CSV 而不是 Excel 文件(您暗示您的问题可能就是这种情况)。read.csv读取分隔的文本文件并且无法处理 Excel 文件(.xls 或 .xlsx)。

如果它是一个分隔的文本文件,那么查看错误消息看起来像您的 CSV(嗯,制表符分隔值文件)可能有一些read.csv()无法处理的空列名。

然后第二个警告还认为文件的最后一行不完整,这可能是由于在某些字段为空时输出文件以组合分隔符引起的。

它们是警告,因为它们不会停止程序或退出程序,但它表示事情可能不像您预期​​的那样。

于 2014-09-16T15:39:58.523 回答
1

我遇到了类似的问题,但您可以尝试使用 skipNul 参数使用以下代码:

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

这里要注意的主要事情是 skipNul 参数。在这种情况下,我使用了 col.names,因为我注意到在这种情况下正确识别列名存在问题。

如果您仍然无法正确获取它,请告诉我。

继续编码!

于 2021-08-16T16:41:46.610 回答
0

您尝试读取的文件可能是 UTF-16 编码的文件,就好像它没有经过如此编码一样。尝试使用 fileEncoding = "UTF-16" 参数

于 2015-03-31T06:56:34.527 回答
0

一个可能的原因是意外地尝试将别名加载到文件而不是文件本身。在 R for Mac 中,尝试read.csv在别名上使用会出现错误。

于 2015-12-14T19:06:45.867 回答