Sometimes in R programming, You need to create blank datasets to add data to later on. when dealing with data frames it is necessary to make sure that you set up the right number of column names otherwise you will get the “error in dimnames(x) = dn : length of ‘dimnames’ [2] not equal to array extent” error message. Fortunately, it is an easy problem to fix.

### Description of the error

This error message occurs when you are setting up a blank matrix or a data frame using a blank matrix and the number of column names you supply is fewer than the number of columns that you defined. It is easy to get this error message because this is an unusual way of setting up a data frame, as a result in the process you may leave out one or more column names from the vector defining them. In such cases correcting the problem is simple because all you need to do, if everything else is right, is add the needed column names.

### Explanation of the error

Here are two code examples that produce our error message. They each show it being caused under slightly different circumstances.

> df = data.frame(matrix(vector(),10,5, dimnames=list(c(), c(“A”, “B”, “C”, “D”))))

Error in matrix(vector(), 10, 5, dimnames = list(c(), c(“A”, “B”, “C”, :

length of ‘dimnames’ [2] not equal to array extent

This example is a basic data frame that produces our error message. Note that we have set the number of columns as five, but we only provide four column names.

> work = data.frame(matrix(vector(),14,7, dimnames=list(c(), c(“Bob”, “Sue”, “Tom”, “Becky”, “Ann”, “Jim”))))

Error in matrix(vector(), 14, 7, dimnames = list(c(), c(“Bob”, “Sue”, :

length of ‘dimnames’ [2] not equal to array extent

In this example, we use more meaningful column names. We defined the number of columns as seven, but we only supply six column names.

### How to fix the error

Here we have two examples where we fixed the problem. In both cases, it is a simple matter of adding another column name.

> df = data.frame(matrix(vector(),10, 5, dimnames=list(c(), c(“A”, “B”, “C”, “D”, “E”))))

> df

A B C D E

1 NA NA NA NA NA

2 NA NA NA NA NA

3 NA NA NA NA NA

4 NA NA NA NA NA

5 NA NA NA NA NA

6 NA NA NA NA NA

7 NA NA NA NA NA

8 NA NA NA NA NA

9 NA NA NA NA NA

10 NA NA NA NA NA

In this example, we simply added a call named “E” to fix the problem. The result is a perfectly good data frame of missing values.

> work = data.frame(matrix(vector(),14,7, dimnames=list(c(), c(“Bob”, “Sue”, “Tom”, “Sindy”, “Ann”, “Jim”, “Al”))))

> work

Bob Sue Tom Sindy Ann Jim Al

1 NA NA NA NA NA NA NA

2 NA NA NA NA NA NA NA

3 NA NA NA NA NA NA NA

4 NA NA NA NA NA NA NA

5 NA NA NA NA NA NA NA

6 NA NA NA NA NA NA NA

7 NA NA NA NA NA NA NA

8 NA NA NA NA NA NA NA

9 NA NA NA NA NA NA NA

10 NA NA NA NA NA NA NA

11 NA NA NA NA NA NA NA

12 NA NA NA NA NA NA NA

13 NA NA NA NA NA NA NA

14 NA NA NA NA NA NA NA

In this example, we added the column name “Al” to bring the number of column names up to seven. This fixes the problem by producing a perfect data frame of missing values.

This is a pretty interesting error message to encounter. While it is easy to stumble into, it is also easy to fix. Fixing this problem simply requires adding the number of column names that you need. So if you encounter this error message do not feel bad, all you need to do is add one or more column names.