导入数据集时出现问题:`scan(...)错误:第1行没有145个元素` [英] Issue when importing dataset: `Error in scan(...): line 1 did not have 145 elements`

查看:681
本文介绍了导入数据集时出现问题:`scan(...)错误:第1行没有145个元素`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用read.table()将数据集导入R:

I'm trying to import my dataset in R using read.table():

Dataset.df <- read.table("C:\\dataset.txt", header=TRUE)

但是我收到以下错误消息:

But I get the following error message:

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

这是什么意思,我该如何解决?

What does this mean and how can I fix it?

推荐答案

此错误是不言自明的.数据文件的第一行(或第二行,视情况而定,因为您使用的是header = TRUE)似乎丢失了数据.

This error is pretty self-explanatory. There seem to be data missing in the first line of your data file (or second line, as the case may be since you're using header = TRUE).

这是一个小例子:

## Create a small dataset to play with
cat("V1 V2\nFirst 1 2\nSecond 2\nThird 3 8\n", file="test.txt")

R自动检测到它应该包含行名加上两列(3个元素),但是在第2行上找不到3个元素,因此会出现错误:

R automatically detects that it should expect rownames plus two columns (3 elements), but it doesn't find 3 elements on line 2, so you get an error:

read.table("test.txt", header = TRUE)
# Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
#   line 2 did not have 3 elements

查看数据文件,看是否确实存在问题:

Look at the data file and see if there is indeed a problem:

cat(readLines("test.txt"), sep = "\n")
# V1 V2
# First 1 2
# Second 2
# Third 3 8

可能需要手动更正,或者我们可以假定第二"行中的第一个值应该在第一列中,而其他值应该是NA.在这种情况下,fill = TRUE足以解决您的问题.

Manual correction might be needed, or we can assume that the value first value in the "Second" row line should be in the first column, and other values should be NA. If this is the case, fill = TRUE is enough to solve your problem.

read.table("test.txt", header = TRUE, fill = TRUE)
#        V1 V2
# First   1  2
# Second  2 NA
# Third   3  8


R也足够聪明,即使缺少行名也能弄清楚它需要多少个元素:


R is also smart enough to figure it out how many elements it needs even if rownames are missing:

cat("V1 V2\n1\n2 5\n3 8\n", file="test2.txt")
cat(readLines("test2.txt"), sep = "\n")
# V1 V2
# 1
# 2 5
# 3 8
read.table("test2.txt", header = TRUE)
# Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
#   line 1 did not have 2 elements
read.table("test2.txt", header = TRUE, fill = TRUE)
#   V1 V2
# 1  1 NA
# 2  2  5
# 3  3  8

这篇关于导入数据集时出现问题:`scan(...)错误:第1行没有145个元素`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆