R aplicar error: error en as.matrix.data.frame ()
Me encuentro con un error desconcertante. Estoy usando la siguiente función para eliminar filas de un marco de datos que contiene una observación de NA en cualquier columna
##### removes NA'd rows from a dataFrame
wipeNArows<-function(X){
rowsToDelete<-unique(unlist(apply(apply(X,2,is.na),2,which)))
if (length(rowsToDelete)>0){
return (X[-rowsToDelete,])
}
else{
return (X)
}
}
Esta función funciona bien normalmente, por ejemplo, un ejemplo reproducible es:
testFrame<-data.frame(x=rpois(20,10),y=rpois(20,10),z=rpois(20,10))
rowsToDelete<-sample(1:nrow(testFrame),5,FALSE)
testFrame$x[rowsToDelete]<-NA
testFrame
wipeNArows(testFrame) ### removes the rows where NA is encountered
Ahora tengo un marco de datos que contiene alrededor de 2993 filas. Cuando paso este marco de datos a través de la función, me encuentro con el siguiente error:
Error in apply(apply(X, 2, is.na), 2, which) :
error in evaluating the argument 'X' in selecting a method for function 'apply': Error in as.matrix.data.frame(X) :
dims [product 14965] do not match the length of object [14974]
Gracias por las respuestas,