jeudi 28 avril 2016

condition has length > 1 - if or ifelse? Vector or matrix?

I create a matrix

y <- matrix(as.numeric(x > mean(x)), nrow = nrow(x))

and would like to calculate certain predictors on this data. However, some conditions do not have enough observations and I get NA (always 80 of them). That is OK, but I would like the other elements to be calculated. When I write:

if (is.na(y) != 80) {
    corr <- cor(as.numeric(y),as.numeric(z))} 
else { data$corr[trial] <- NA }

I get the error message that the condition has length > 1 and only the first element will be used. So apparently R sees y as a vector? But when I type is.vector(y) the output is FALSE. How can I adapt my code? Or is my thinking wrong?

Aucun commentaire:

Enregistrer un commentaire