# Fixing the R error in apply dim(x) must have a positive length

This error message occurs when you try to use the apply function on subparts of a data frame or matrix. The simplest way to fix it is simply applying the function to the entire r object. However, there are ways to get the results you are looking for that form other ways of fixing this error message.

### Description of the error

The apply function is a function that applies other functions to an entire object such as data frames or matrixes. When this function is used each column in a data frame is treated as a vector. Furthermore, r coerces them to atomic vector. This function has variances such as lapply which supplies the output as a list, and mapply which allows the application of more than one argument to a function. It is not part of a package but you do have to watch out for required fields. This error message occurs when you try to use this function on a single data frame column or matrix subset. There are several ways of fixing this problem and the best one to go with depends upon what you are trying to do.

### Explanation of the error

Here are three examples of the apply function being used that helped illustrate this error message.

> df = data.frame(A = c(1, 5, 8, 2, 7),
+ B = c(1, 2, 3, 4, 5),
+ D = c(5, 6, 7, 8, 9))
> apply(df\$A, 2, mean)
Error in apply(df\$A, 2, mean) : dim(X) must have a positive length

This r code shows that this error message does not occur because of a quantity problem, but the fact that a single column in the data frame is being referred to.

> df = data.frame(A = c(1, 5, 8, 2, 7),
+ B = c(1, 2, NA, 4, 5),
+ D = c(5, 6, 7, 8, 9))
> apply(df, 2, mean)
A B D
4.6 NA 7.0

In this example, we see how the apply function handles na values. In this case, it simply produces na value for the column, showing that it is not the source of our message.

> df = data.frame(A = c(complex(real = 5,imaginary = 7), complex(real = 4,imaginary = 7), complex(real = 0,imaginary = 1), 2, 7),
+ B = c(1, 2, 3, 4, 5),
+ D = c(5, 6, 7, 8, 9))
> apply(df, 2, mean)
A B D
3.6+3i 3.0+0i 7.0+0i

In this example, we see that having a complex numeric vector also works because the mean function will work with any numeric value. As a result, this cannot be the cause of our error message.

### How to fix the error

These r code examples show three ways of fixing this error message.

> df = data.frame(A = c(1, 5, 8, 2, 7),
+ B = c(1, 2, 3, 4, 5),
+ D = c(5, 6, 7, 8, 9))
> apply(df, 2, mean)
A B D
4.6 3.0 7.0

This is the simplest way to fix this problem and that is to simply refer to the entire data frame.

> df = data.frame(A = c(1, 5, 8, 2, 7),
+ B = c(1, 2, 3, 4, 5),
+ D = c(5, 6, 7, 8, 9))
> apply(df[c(‘B’,’D’)], 2, mean)
B D
3 7

In this example, we are referring to two columns in the data frame rather than one, and this prevents the error message.

> df = data.frame(A = c(1, 5, 8, 2, 7),
+ B = c(1, 2, 3, 4, 5),
+ D = c(5, 6, 7, 8, 9))
> mean(df\$A)
 4.6

In this example, we skip the apply function and refer directly to the column in the mean function. Effectively, the apply function forms a type of loop.

This error message occurs because working with the apply function requires knowledge, not only of how that function works but how the function that you are using it with works. You cannot just drop any element in as an argument. The apply function is looking for a data frame or matrix, not a vector. Once you understand it, this is an easy mistake to avoid. Fortunately, if you do make it, it is an easy one to fix.

Scroll to top