vendredi 10 septembre 2021

Find min of vector based on conditional r

I'm fairly new in r, and the moment I'm writing a function, where a part of it want to find the minimum value index of a vector if that index is not part of another "checked" vector. I'll try to illustrate below.

node_vector <- c(0, 45, Inf, Inf, Inf, Inf)
que <- c(2,3,4,5,6)
visited<- c(1)

while(length(que) > 0){
        
   for(i in seq_along(node_vector)){
        if(!i %in% visited && which.min(node_vector[i]) == i){
           current_node <- which.min(distance_vector[i]) == i)
        }
   }
   que <- que[-current_node]
   visited <- c(visited, current_node)
}

As of now I am getting current_node <- 1 all the time. How would I get it to recognize that pos 1 has already been visited, and instead set current_node <- 2 the first time I run it?

I'm sorry if the title is confusing, I'm not quite sure what to call it:)

Aucun commentaire:

Enregistrer un commentaire