Yun Hyunsoo
Yun Hyunsoo

Reputation: 71

how to store for loop result to dataframe with if next condition

I am quite new to R so apologize if I am asking something obvious. I am trying to do a for loop for 143,635 loops. But in the loop, there are some conditions where I skip my loop.

Here is my code below (~~~ are some codes for the loop)

j=1
result<-NULL

for(j in 1:143635)  
{~~~ 
if(nrow(OD_Routetable)<2) next 

~~
result1<-(prd_v==test$route)/nrow(test)*100

result<-rbind(result,result1)

}

is there a way I can save my for loop results into a data frame of 143,635 rows while filling the data frame with value "100" for the skipped loops? would if else be more usefull?

Upvotes: 0

Views: 52

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101403

How about a for loop like below?

result<-c()

for(j in 1:143635)  
{~~~ 
if(nrow(OD_Routetable)<2) {
    result[j] <- 100
} else {
    result[j]<-(prd_v==test$route)/nrow(test)*100
}
~~

}

Upvotes: 1

Related Questions