Fixing The R error in table() : all arguments must have the same length

This error message occurs when using the table function with multiple arguments. It means that at least one argument has a length that is different from the required argument. The required argument is the r object that you are evaluating. To fix this problem you need to find the length of all the arguments in the argument list. This will show you which extra argument has an incorrect length so that you can correct it.

Description of the error

The default function call of the table function has only one argument and that is the object being evaluated. The function definition allows additional arguments to set parameters and other purposes. These arguments can be keyword arguments, format arguments, parameter arguments, and more. Whenever you use an additional argument, its value has to have the same length as the input argument. Any function argument has to meet this condition, or you will get our error message rather than a return value. The object being evaluated can be a data frame, a vector, a character string, or an array. The input values can be a numeric value, a character value, or even a missing value just as long as each argument value has the same length. An argument can even be a function with its values in parentheses. You will get a similar error message when using the aggregate function under the same situation.

Explanation of the error

Here is an example of code that shows what causes this error message.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> xyz
z x y
1 A 1 1
2 B 2 3
3 C 3 5
4 D 4 7
5 E 5 3
6 C 3 9
> length(xyz)
[1] 3
> length(seq(0, 10, by = 2))
[1] 6
> table(xyz, seq(0, 10, by = 2))
Error in table(xyz, seq(0, 10, by = 2)) :
all arguments must have the same length

In this example, the table function includes an extra element beyond the required argument. The two arguments used are the xyz data frame and the function seq(0, 10, by = 2). Furthermore, the length function is applied to both showing that xyz has a length of three and seq has a length of six. It is this length mismatch that causes our error message.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> xyz
z x y
1 A 1 1
2 B 2 3
3 C 3 5
4 D 4 7
5 E 5 3
6 C 3 9
> t = table(xyz)

The fact that the error message is caused by the optional argument is further illustrated by removing it. When this is done, we no longer get the error message, however, this does not really solve the problem.

How to fix the error

Here we have an example of code that actually fixes the problem, rather than just eliminating it.

> x = c(1:5,3)
> y = c(1,3,5,7,3,9)
> z = c(“A”, “B”, “C”, “D”, “E”, “C”)
> xyz = data.frame(z, x, y)
> xyz
z x y
1 A 1 1
2 B 2 3
3 C 3 5
4 D 4 7
5 E 5 3
6 C 3 9
> length(xyz)
[1] 3
> length(seq(0, 5, by = 2))
[1] 3
> table(xyz, seq(0, 5, by = 2))

xyz 0 2 4
c(1, 2, 3, 4, 5, 3) 1 1 0
c(1, 3, 5, 7, 3, 9) 0 0 1

In this example, we made sure that the function parameter, that requires arguments to have the same length, is satisfied. If you look at the length functions, you will see that both arguments have the same length.

This is a tricky error message because the problem is not easy to see. When it occurs use the length function on the table function arguments to see their lengths. After that, all you need to do is adjust the offending argument. It is a problem that is easy to fix, but tricky to diagnose.

Scroll to top