jeudi 15 février 2018

Error while using ifelse in apply function - R

Hello I have the following code;

valorNegativos = function (vela) {
  for(a in 1:length(vela)) {
    apply(vela[[a]], 2,  function(col) ifelse( length(as.numeric(col) < 0) == length(vela[[a]]$FECHA), print(paste0("Correct data")), stop("ERROR: Negative values"))
  }
}

vela, is a list of dataframes.

What I have to do with this function is to prove if all the columns values from all my dataframes are positive ( > 0 ) and to check if the number of values is equal to the value count from one column $FECHA ( date ).

The problem is that I am getting the two posibilites from the ifelse.

[1] "Correct data"
 Show Traceback

 Rerun with Debug
 Error in ifelse(length(as.numeric(col) > 0) == length(vela[[a]]$FECHA,  : 
  ERROR: Negative values

Any idea?

Aucun commentaire:

Enregistrer un commentaire