mercredi 16 septembre 2020

Error ... missing value where TRUE/FALSE needed

I've been trying to write a code to sort a vector in R, but I keep getting this error message: Error in if (data[j] < data[k]) { : missing value where TRUE/FALSE needed

This is my sort function so far:

sortscore <- function(data){
   new <- 0
   x <- data
  
   for (i in 1:length(data)){
      new[i] <- minscore(x)
      x <- x[x!=minscore(x)]
      x
   }
   new
}

This is the minscore function:

minscore <- function(data){
  j <- 1;k <- j+1; min <- 0
  repeat {
    if(data[j]<data[k]){
      min <- data[j]
      k <- k+1
    }
    else{
      min <- data[k]
      j <- k
      k <- k+1
    }
    if(k==length(data)+1) break
  }
  return(min)
}

I can only use length() function for a built-in function, hence the need for a sort function. Please help me understand.

Aucun commentaire:

Enregistrer un commentaire