Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R? 2

Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?

The error “duplicate ‘row.names’ are not allowed” in R occurs when there are multiple rows in a dataset that share the same name. This can cause conflicts and errors when trying to manipulate or analyze the data. To fix this error, the duplicate row names must be identified and changed to unique names. This can be done by using the “make.unique()” function or manually renaming the rows. It is important to have unique row names in order to properly organize and analyze data in R.

Fix in R: duplicate ‘row.names’ are not allowed


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:

Additional Resources

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

Cite this article

stats writer (2024). Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/can-someone-explain-how-to-fix-the-error-duplicate-row-names-are-not-allowed-in-r/

stats writer. "Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/can-someone-explain-how-to-fix-the-error-duplicate-row-names-are-not-allowed-in-r/.

stats writer. "Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/can-someone-explain-how-to-fix-the-error-duplicate-row-names-are-not-allowed-in-r/.

stats writer (2024) 'Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/can-someone-explain-how-to-fix-the-error-duplicate-row-names-are-not-allowed-in-r/.

[1] stats writer, "Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. Can someone explain how to fix the error “duplicate ‘row.names’ are not allowed” in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top