How to fix the “duplicate ‘row.names’ are not allowed” error in R?

The “duplicate ‘row.names’ are not allowed” error in R can be fixed by setting the row.names argument in the data frame as FALSE. This will remove the row names and the error should no longer appear.


One error you may encounter in R is:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  duplicate 'row.names' are not allowed 

This error usually occurs when you attempt to read a CSV file into R that contains commas at the end of every row in the file except the header row.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we have the following CSV file called my_data.csv:

Notice that there are commas at the end of every row in the file except the header row.

Now suppose we attempt to import this file into R:

#attempt to import CSV into data frame
df <- read.csv('my_data.csv')

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  duplicate 'row.names' are not allowed

We receive an error because there are commas at the end of every row in the file except the header row, which causes R to think that the first column of values are the row names.

Since two of the rows have the same starting value (4), R thinks that there are duplicate row names.

How to Fix the Error

The way to fix this error is to simply use row.names=NULL when importing the file:

#import CSV file into data frame
df <- read.csv('my_data.csv', row.names=NULL)

#view data frame
df

  row.names column1 column2 column3
1         4       5       7      NA
2         4       2       1      NA
3         7       9       0      NA

We are able to successfully import the CSV file, but the column names are off.

To fix this, we can modify the column names and then drop the last column:

#modify column names
colnames(df) <- colnames(df)[2:ncol(df)]

#drop last column
df <- df[1:(ncol(df)-1)]

#view updated data frame
df

  column1 column2 column3
1       4       5       7
2       4       2       1
3       7       9       0

The data frame is now in the correct format.

Related:

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: longer object length is not a multiple of shorter object length

x