R Error in scan: Line 1 did not have X Elements (3 Examples)


In this R tutorial you’ll learn how to handle the error in scan: line 1 did not have X elements.

Table of contents:

Let’s dig in:


Example 1: Reproduce the Error in scan: line 1 did not have X elements

In this example, I’ll explain how to replicate the error message “scan: line 1 did not have X elements” in R.

Let’s assume that we want to import the txt-file data set shown in Figure 1:


r graph figure 1 txt file


Then, we could usually use the read.table function as shown below:

read.table("C:/Users/Joach/Desktop/My Folder/my_data.txt",           # txt file with empty cells
           header = TRUE)
# Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
#   line 1 did not have 3 elements

Unfortunately, the error message “line 1 did not have 3 elements” occurred. The reason for this is that our data is not complete, i.e. some data cells are empty.

Have a look at the first data row (i.e. the row after the header). This row contains only two values, not three values as the other rows.

In the following two examples, I’ll show two different solutions on how to fix this error message. So keep on reading!


Example 2: Fix the Error by Formatting the txt File

In this example, I’ll show how to solve the error in scan: line 1 did not have X elements by formatting our txt input file.

Have a look at the following updated txt-file that I have stored in a new txt-file called “my_data_complete.txt”:


r graph figure 2 txt file


As you can see, I have inserted an NA-value in the second column and the first row of our data.

Let’s import these data into R:

read.table("C:/Users/Joach/Desktop/My Folder/my_data_complete.txt",  # Properly formatted txt file
           header = TRUE)
#   x1 x2 x3
# 1  1 NA  9
# 2  2  5  8
# 3  3  5  7

Works fine! The data cell in the second variable and the first row is set to a missing value (i.e. NA).


Example 3: Fix the Error Using the fill Argument

The read.table function provides an even simpler solution for importing txt-data with empty cells into R.

We can simply specify the fill argument to be equal to TRUE:

read.table("C:/Users/Joach/Desktop/My Folder/my_data.txt",           # Using fill argument
           header = TRUE,
           fill = TRUE)
#   x1 x2 x3
# 1  1  9 NA
# 2  2  5  8
# 3  3  5  7

The previous R coded also didn’t lead to any errors. However, please note that the NA value was inserted at the last position of the first row (i.e. the third column). Make sure to check whether this makes sense in your specific data situation!


Video & Further Resources

Do you need further info on the examples of this article? Then you could have a look at the following video of my YouTube channel. I’m explaining the R programming code of this article in the video:


The YouTube video will be added soon.


In addition, you might read the other tutorials of this website. I have released numerous articles already.


In summary: On this page, I showed how to solve problems caused by the error in scan: line 1 did not have X elements in R programming. In case you have additional comments or questions, tell me about it in the comments.


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.