Fixing the R Error – duplicate ‘row.names’ are not allowed

The duplicate ‘row.names’ are not allowed error message occurs when you are adding row names to a data frame, and you have duplicate row names. The way to fix this problem is to make sure that each row name is unique. This can be accomplished either manually or using the make.names function. Adding row names to a data frame makes it print out looking more like a table, causing it to be easier to understand.

Description of the R error – duplicate ‘row.names’ are not allowed

While both duplicate column names and duplicate row names are not allowed a duplicate column name does not cause an error message because column names and the header row are automatically adjusted by the data frame function. When you add row names you are changing the header field with another function. When dealing with an individual column value, you can use duplicated data, but a label cannot be duplicated. As a result, adding duplicate row names will produce our error message. Fixing the problem is a simple matter of making sure that each row name is different. This difference can be achieved both manually and automatically using the make.names function.

Explanation of the R error

Here are two code examples that help illustrate why this error message occurs.

> rn = c(“Apple”,”Box”,”clause”,”Dot”)
> x = c(1:4)
> y = c(1,3,5,9)
> z = c(“E”, “F”, “G”, “H”)
> xyz = data.frame(x, y, z)
> xyz
x y z
1 1 1 E
2 2 3 F
3 3 5 G
4 4 9 H
> rownames(xyz) = rn
> xyz
x y z
Apple 1 1 E
Box 2 3 F
clause 3 5 G
Dot 4 9 H

In this example, we see how this process works properly because it is using unique rows’ names. As a result, we do not get an error message.

> rn = c(“Apple”,”Apple”,”Apple”,”Box”)
> x = c(1:4)
> y = c(1,3,5,9)
> z = c(“E”, “F”, “G”, “H”)
> xyz = data.frame(x, y, z)
> xyz
x y z
1 1 1 E
2 2 3 F
3 3 5 G
4 4 9 H
> rownames(xyz) = rn
Error in `.rowNamesDF=`(x, value = value) :
duplicate ‘row.names’ are not allowed
In addition: Warning message:
non-unique value when setting ‘row.names’: ‘Apple’

In this example, we see how to produce our error message. In this case, we have the row name “Apple” duplicated three times and we get our error message as a result. It is interesting to note that you will get our error message even if the duplicate row names are missing values.

How to fix the R error – duplicate ‘row.names’ are not allowed

Here we have two examples illustrating how to fix this error message.

> rn = c(“Apple”,”Apple”,”Apple”,”Box”)
> x = c(1:4)
> y = c(1,3,5,9)
> z = c(“E”, “F”, “G”, “H”)
> xyz = data.frame(x, y, z)
> xyz
x y z
1 1 1 E
2 2 3 F
3 3 5 G
4 4 9 H
> rownames(xyz) = unique(rn)
Error in `.rowNamesDF=`(x, value = value) : invalid ‘row.names’ length

This one shows an incorrect way, which would be a natural inclination when dealing with a duplicate row. This example does not fix the problem because the unique function will remove duplicates. While it will produce unique value row names, it needs to drop duplicates. This causes another error message because there needs to be a row name for each row.

> rn = c(“Apple”,”Apple”,”Apple”,”Box”)
> x = c(1:4)
> y = c(1,3,5,9)
> z = c(“E”, “F”, “G”, “H”)
> xyz = data.frame(x, y, z)
> xyz
x y z
1 1 1 E
2 2 3 F
3 3 5 G
4 4 9 H
> rownames(xyz) = make.names(rn, unique=TRUE)
> xyz
x y z
Apple 1 1 E
Apple.1 2 3 F
Apple.2 3 5 G
Box 4 9 H

In this example, we fix the error message by using a function that adjusts the duplicate names by adding a number. This will come in handy in cases where you do not have control over the vector being used to provide the row names.

This is an easy-to-understand error message, it is a case where the message gives you meaningful information on what is going on. It is also an easy problem to fix because you can make the fix both manually and automatically. The key is making sure, that when adding row names to a data frame, that all the names are unique.

Scroll to top
Privacy Policy