How to Solve R Error: more columns than column names

by | Programming, R, Tips

This error occurs when you try to read a CSV file into R using read.table() and do not specify the correct separator. You can solve this error by specifying the sep argument for the read.table() function call. For example,

df <- read.table("pizzas.csv", header=TRUE, sep=",")

This tutorial will go through the error in detail and how to solve it with code examples.


Example

Let’s look at an example of reproducing the error. Consider the following CSV file called pizza_prices.csv:

pizza,price
margherita, 7.99
pepperoni, 8.99
four cheeses, 10.99
funghi, 8.99

Next, we will attempt to import the CSV data into R using the read.table() function:

# Try to import CSV into data frame

df <- read.table("pizza_prices.csv", header=TRUE)

Let’s run the code to see what happens:

Error in read.table("pizza_prices.csv", header = TRUE) : 
  more columns than column names

The error occurs because we did not specify the sep argument for the read.table() function. If we do not specify a separator, the default whitespace is used.

There are whitespaces in between the values in the rows but not in the header, therefore the read.table() interprets the header as having one column and the rows as having two columns.

Solution

We can solve this error by specifying the separator value of ',' to the sep argument. Let’s look at the revised code:

df <- read.table("pizza_prices.csv", header=TRUE, sep=",")

df

Let’s run the code to get the data frame:

   pizza price
1   margherita  7.99
2    pepperoni  8.99
3 four cheeses 10.99
4       funghi  8.99

We can also use the read.csv() function to import the file, which is more suitable as we know the data is stored in a CSV file.

Let’s look at the revised code:

df <- read.csv("pizza_prices.csv", header=TRUE)

df

Let’s run the code to get the data frame:

   pizza price
1   margherita  7.99
2    pepperoni  8.99
3 four cheeses 10.99
4       funghi  8.99

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles: 

For more on handling Data Frames in R, go to the article:

How to Add an Index column to a Data Frame in R

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨