Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - argument is of length zero in if statement

Tags:

r

I am having a little problem with R and I am not sure why. It is telling me that this line: if(temp > data[[k]][[k2]]) { is of argument length 0. Here is the block which is not that big:

for(k in 1:length(data)) {        temp <- 0       for(k2 in 3:length(data[[k]])) {            print(data[[k]][[k2]])            if(temp > data[[k]][[k2]]) {                 temp <- data[[k]][[k2]]             }             fMax[k] <- temp            k2 <- k2 + 1       }  k <- k + 1  } 

example of what is in data[[k]][[k2]]:

[1] "3050" [1] "3051" [1] "3054" [1] "3054" [1] "3052" [1] "3053" [1] "3059" [1] "3059" [1] "3057" [1] "3060" [1] "3063" [1] "3060" [1] "3068" [1] "3067" [1] "3079" [1] "3085" [1] "3094" [1] "3107" [1] "3121" [1] "3135" [1] "3147" [1] "3161" [1] "3200" [1] "3237" [1] "3264" [1] "3274" [1] "3284" [1] "3289" [1] "3292" [1] "3300" [1] "3301" [1] "3303" [1] "3306" [1] "3310" [1] "3312" [1] "3313" [1] "3319" [1] "3314" [1] "3318" [1] "3318" [1] "3320" [1] "3322" [1] "3322" [1] "3322" [1] "3328" [1] "3332" [1] "3338" [1] "3350" [1] "3358" [1] "3378" [1] "3395" [1] "3402" [1] "3875" [1] "3950" [1] "3988" [1] "4018" [1] "4039" [1] "4048" [1] "4057" [1] "4062" [1] "4067" [1] "4076" [1] "4082" [1] "4085" [1] "4092" [1] "4098" [1] "4099" [1] "4101" [1] "4107" [1] "4119" [1] "4139" [1] "4164" [1] "4231" [1] "4347" [1] "4559" 

Thanks - Sam

like image 271
user3558177 Avatar asked Dec 08 '14 02:12

user3558177


People also ask

How do you fix an argument of length zero in R?

46.7 argument is of length zero This could be fixed by changing “E” in the if staement to a valid column name like “D.”

Is Na an argument of length zero R?

The RStudio console returns the error message “argument is of length zero”. The reason for this is that we have used an empty data object in the logical condition within the if-statement (i.e. x1 > 5). This is not possible in R and therefore the RStudio console returns an error.

How do you use an argument in R?

Adding Arguments in R We can pass an argument to a function while calling the function by simply giving the value as an argument inside the parenthesis.


1 Answers

"argument is of length zero" is a very specific problem that comes from one of my least-liked elements of R. Let me demonstrate the problem:

> FALSE == "turnip" [1] FALSE > TRUE == "turnip" [1] FALSE > NA == "turnip" [1] NA > NULL == "turnip" logical(0) 

As you can see, comparisons to a NULL not only don't produce a boolean value, they don't produce a value at all - and control flows tend to expect that a check will produce some kind of output. When they produce a zero-length output... "argument is of length zero".

(I have a very long rant about why this infuriates me so much. It can wait.)

So, my question; what's the output of sum(is.null(data[[k]]))? If it's not 0, you have NULL values embedded in your dataset and will need to either remove the relevant rows, or change the check to

if(!is.null(data[[k]][[k2]]) & temp > data[[k]][[k2]]){     #do stuff } 

Hopefully that helps; it's hard to tell without the entire dataset. If it doesn't help, and the problem is not a NULL value getting in somewhere, I'm afraid I have no idea.

like image 109
Oliver Keyes Avatar answered Sep 20 '22 03:09

Oliver Keyes