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: 

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!