dimanche 29 mars 2015

R “argument is of length zero” in if(condition)

Very new to R, would appreciate if you could tell me what I’m doing wrong.


This was my initial code:



count <- 0
for (i in 1:nrow(data))
{
if(outcome[i]==data[1,i])
inc(count) <- 1 #package Hmisc
}


where outcome and data[1] are vectors with binary values eg. [1, 0, 0, 1, 1, 0..]


I got the result ‘argument is of length zero’ with the if statement. Tested it individually with rows that matched/mismatched, discovered that the result shows up only in the latter.


So I amended my code to:



count <- 0
for (i in 1:nrow(data))
{
if(!is.null(outcome[i]==data[1,i]))
inc(count) <- 1 #package Hmisc
}


Now the value that count returned is the number of ‘for' iterations.


Doesn’t this mean R is NOT returning a null value for ANY mismatch in the new code?? How is this happening? Solutions please?


Aucun commentaire:

Enregistrer un commentaire