Issue when importing dataset: `Error in scan(...): line 1 did not have 145 elements`
I'm trying to import my dataset in R using
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?
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
r: Issue when importing dataset: `Error in scan(): line 1 did not have , Error while importing dataset: "Scan error (): row 1 did not contain 145 elements" It seems that there is no data in the first line of your data file (or in the second (3 elements), but it does not find 3 elements in row 2, so you get an error message: There seems to be a slight issue with formatting some files that cause R to Hello silkeverhelle! We believe that this post does not fit the main topic of this site. No longer relevant. For this reason we have closed your question.
When running into this error and reviewing my dataset which appeared to have no missing data, I discovered that a few of my entries had the special character "#" which derailed importing the data. Once I removed the "#" from the offending cells, the data imported without issue.
♀️ 🤘 Error importing dataset: "Scan error (): row 1 did not have , Here are the solutions: You could have also set read.table(, comment.char = "") to turn off the interpretation of comments in the file. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.
I encountered this issue while importing some of the files from the Add Health data into R (see: http://www.icpsr.umich.edu/icpsrweb/ICPSR/studies/21600?archive=ICPSR&q=21600 ) For example, the following command to read the DS12 data file in tab separated .tsv format will generate the following error:
ds12 <- read.table("21600-0012-Data.tsv", sep="\t", comment.char="", quote = "\"", header=TRUE) Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 2390 did not have 1851 elements
It appears there is a slight formatting issue with some of the files that causes R to reject the file. At least part of the issue appears to be the occasional use of double quotes instead of an apostrophe that causes an uneven number of double quote characters in a line.
After fiddling, I've identified three possible solutions:
Open the file in a text editor and search/replace all instances of a quote character " with nothing. In other words, delete all double quotes. For this tab-delimited data, this meant only that some verbatim excerpts of comments from subjects were no longer in quotes which was a non-issue for my data analysis.
With data stored on ICPSR (see link above) or other archives another solution is to download the data in a new format. A good option in this case is to download the Stata version of the DS12 and then open it using the read.dta command as follows:
library(foreign) ds12 <- read.dta("21600-0012-Data.dta")
A related solution/hack is to open the .tsv file in Excel and re-save it as a tab separated text file. This seems to clean up whatever formatting issue makes R unhappy.
None of these are ideal in that they don't quite solve the problem in R with the original .tsv file but data wrangling often requires the use of multiple programs and formats.
R: Error In Scan Line 7 Did Not Have 3 Elements, does not deal with statistics, as, in our experience, teaching statistics and R at all fields need to import data, massage data, make graphs, and, finally, 9.1.1 Errors in the Source File . and  are the index numbers of the first element on a new line. frame, whereas the scan function stores the data as a matrix. Reading data with blank elements. Hi, I have a dataset saved in *.csv format, that contains 13 columns (the first column being the title name and the rest experiments) and about 2500 rows.
If you are using linux, and the data file is from windows. It probably because the character ^M Find it and delete. done!
Getting Data into R, R-project.org/package=gdata):. Gregory The primary function to import from a text file is scan, and There are a number of issues that need to be considered in writing out a data frame to a text file. If the options to read.table are specified incorrectly, the error message will usually be of line 1 did not have 5 elements or. Community: If one has a data set that does not have true headers (i.e., line1 is not all headers): Line1 Table No 1 Line 2 ID AMT DV How should the following header code be edited? datr<-read.table(file="popfo7n_100.fit",header=T) Would the same edit be correct for the next line of code?
One of my variables was categorical with one alternative being multi string ("no event"). When I used read.table, it assumed that the space after the first string meant the end of the data point and the second string was pushed to the next variable. I used sep= "\t" to solve the problem. I was using RStudio in a Mac OX environment. A previous solution was to transform .txt files to .csv in Excel, and afterwards open them with read.csv function.
[PDF] R Data Import/Export - CRAN, read.csv warning 'EOF within quoted string' prevents complete reading of file. Asked 6 years, 4 months ago. Active 1 year, 3 months ago. Viewed 113k times. I have a CSV file (24.1 MB) that I cannot fully read into my R session. When I open the file in a spreadsheet program I can see 112,544 rows. When I read it into R with read.csv I only get
1. You can open the file in a text reader and save it again with the UNIX line breaks. 2. You can use a utility like fromdos or dos2unix to convert the file. 3. You can use a vi command like this to replace the line breaks with the correct ones: :%s/^M/^M/g. Hint: The trick here is in how you enter the command.
1. scan all the csv files with non-standard SAS names - like 'Batch A (1-5000)' or '1.SOURCE_DATA CTRY1' but all the csv files have the same format with in each a first line header but not the same number of rows (25'000 to 50'000 rows).
If the server does have enough physical memory, but you are still seeing this error, it could be that other processes are using too much memory or a configuration issue causes not enough memory to be available for restore. For this class of issues, use the following measures to make more memory available to the restore operation: