I\'m trying to import my dataset in R using read.table()
:
Dataset.df <- read.table(\"C:\\\\dataset.txt\", header=TRUE)
But
Hash #
symbol creating this error, if you can remove the #
from the start of the column name, it could fix the problem.
Basically, when the column name starts with #
in between rows, read.table()
will recognise as a starting point for that row.
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
).
Here's a mini example:
## Create a small dataset to play with
cat("V1 V2\nFirst 1 2\nSecond 2\nThird 3 8\n", file="test.txt")
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
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 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
I have faced same issue while trying to read data from file in R. After figuring out I found out that sep
value is causing this issue. Once I tried this with correct separator it was working as expected.
read.table("file_location/file_name",
sep="." # exact separator as given in file: also "," or "\t" etc.
col_names=c("name_1", "name_2",..))
For others who can't find a solution and know the data isn't missing elements:
I have this issue when I use Excel 2013 to save files as .csv and then try to load them in R using read.table(). The workaround I have found is to paste the data straight from Excel into a .txt document, then open with:
read.table(file.choose(), sep="\t").
I hope this helps.
Beside all the guidance mentioned above,you can also check all the data.
If there are blanks between words, you must replace them with "_"
.
However that how I solve my own problem.