Error messages are irritating to any programmer, but they let you know when you have made a mistake. While the meaning of the error message “argument is of length zero” is not obvious at first, it makes sense once you understand what is causing it. This fact also makes it an easy error to fix.
The Circumstances Of This Error
This error message occurs when using a For Loop with a dataframe using with an incorrect column name.
> df = read.table(text = '
+ A B C D
+ 1 3 20 81 21
+ 2 4 34 13 22
+ 3 5 46 18 23
+ 4 6 42 16 24
+ 5 7 65 26 25', header=TRUE)
>
> for (i in 1:nrow(df1))
+ if(df[i, "A"] == df[i, "E"]) c=0
Error in if (df[i, "A"] == df[i, "E"]) c = 0 : argument is of length zero
In this example, the data frame “df” has no column named “E” so invoking it in the For Loop results in our error message.
What Is Causing This Error?
The cause of this error is the fact that a data frame returns a length of zero when calling a column name that it does not have. So when in the situation above, we called a non-existent column name as argument in a logical statement the compiler returned our error message. The reason for the precise error message makes sense, given the fact that the length of a non-existing column is zero. It may not be obvious at first glance but it makes sense.
How To Fix This Error
Fixing the error message is a simple matter of making sure that you are using a column name that is actually present in the data frame. Getting the wrong column name can be a simple matter of typographical error. For example, if you meant to say “Bob” but accidentally typed “Bb” you will get our error message.
> df = read.table(text = '
+ A B C D
+ 1 3 20 81 21
+ 2 4 34 13 22
+ 3 5 46 18 23
+ 4 6 42 16 24
+ 5 7 65 26 25', header=TRUE)
>
> for (i in 1:nrow(df1))
+ if(df[i, "A"] == df[i, "D"]) c=0
In this example, both references to the data frame have valid column names. This is the key to fixing and preventing this error.
Like many error messages, this one looks more intimidating than it is. Once you understand it, it is easy to avoid. Just make sure that you accurately use the column names that are in your data frame, as well as avoiding typographical errors.