This error message occurs when you try to assign column names to a vector. It occurs because a vector has only one dimension. To fix this problem you need to change the vector into a data frame. This will fix the problem allowing you to work with this data frame.
Description of the R Error
The colnames function is a base r function and not part of a package. This feature deals only with column names, it does not care about any value in the column. It can work with character, logical and numeric columns. The column can be a dummy variable filled with duplicate values or even missing values. It does not care about unique rows or even a duplicate row. This problem occurs because a vector is a single column variable and the colnames function needs at least a second column to supply an output. It will not do a subset of column names to a single column. Therefore, this situation produces an error message.
Explanation of the R error
This sample code shows how to produce this error message.
> x = data.frame(a=c(1, 2, 3, 4, 5, 6, 7, 8, 9,10),
+ b=c(2, 4, 6, 8,10,12,14,16,18,20),
+ c=c(3, 6, 9,12,15,18,21,24,27,30),
+ d=c(4, 8,12,16,20,24,28,32,36,40))
> x
a b c d
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
5 5 10 15 20
6 6 12 18 24
7 7 14 21 28
8 8 16 24 32
9 9 18 27 36
10 10 20 30 40
> y = c(11,22,33,44)
> y
[1] 11 22 33 44
> colnames(y) = colnames(x)
Error in `colnames=`(`*tmp*`, value = c(“a”, “b”, “c”, “d”)) :
attempt to set ‘colnames’ on an object with less than two dimensions
At first, creating a data frame row may seem like a logical vector use, but it does not work. This is because a vector forms a column and not a row. The rest of the example data is a data frame consisting of several columns. The result is that this situation produces our message.
How to fix the R error
In this example, we fix this problem using test data that consists of two data frames.
> x = data.frame(a=c(1, 2, 3, 4, 5, 6, 7, 8, 9,10),
+ b=c(2, 4, 6, 8,10,12,14,16,18,20),
+ c=c(3, 6, 9,12,15,18,21,24,27,30),
+ d=c(4, 8,12,16,20,24,28,32,36,40))
> x
a b c d
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
5 5 10 15 20
6 6 12 18 24
7 7 14 21 28
8 8 16 24 32
9 9 18 27 36
10 10 20 30 40
> y = data.frame(11,22,33,44)
> y
X11 X22 X33 X44
1 11 22 33 44
> colnames(y) = colnames(x)
> y
a b c d
1 11 22 33 44
> rbind(x, y)
a b c d
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
5 5 10 15 20
6 6 12 18 24
7 7 14 21 28
8 8 16 24 32
9 9 18 27 36
10 10 20 30 40
11 11 22 33 44
In this example, we fix the problem by setting up Y as a single row data frame instead of a vector. This allows us to use the colnames function without any problems. Furthermore, we can use the rbind function to create a data frame where Y adds a new row to x.
This is an amazingly simple error message to both understand and fix. While it is an easy mistake to make, once you understand the problem it becomes easy to fix and avoid. The message gives you a good clue to the problem, and this helps make it easier to fix. One thing that will help you to understand this problem is to remember that data frame columns are vectors and as such a vector cannot be substituted for a data frame row. If you understand this simple point, you will not make this mistake again.