Two interdependent for loops in R, strange results

I am trying to calculate something in R for different values โ€‹โ€‹of parameters a and b, where my parameter b should always be less than or equal to a. To do this, I make two loops where I change (from 0 to 4) and then b from 0 to a, but R gets weird b values.

v=c() L<-0 for (a in seq(0, 4, length.out=41)){ for (b in seq(0, a, length.out=(10*a+1))){ L<-L+1 v[L]<-b } } v 

It seems to me that b should always start from 0 to a by 0.1 steps. But this is not always, sometimes the steps are smaller, as can be seen in positions 23-28 of the vector v (for example). Does anyone have an idea why this is so. I can not find a mistake! Thanks!

+4
source share
3 answers

The documentation for seq notes that the length.out value will be rounded. Since a is numeric and therefore associated with some error, it is possible to get another length than you expect, which gives you a weird result.

 for (a in seq(0, 4, length.out=41)[1:7]){ print(paste(as.integer(10*a+1), ceiling(10*a+1))) } # [1] "1 1" # [1] "2 2" # [1] "3 3" # [1] "4 4" # [1] "5 5" # [1] "6 6" # [1] "7 8" 

Pay attention to the last line: you get 8 instead of 7.

To solve this problem, try converting the length to an integer by rounding:

 for (b in seq(0, a, length.out=round(10*a+1))){ 
+6
source

What happened to the old fashioned way to do this?

 v=c() L<-0 for (a in 0:40){ for (b in 0:a){ L<-L+1 v[L]<-b/10 } } v 
+3
source

To add an answer to Peyton, you can see what you actually get here:

 print(seq(0,4,length.out=41)[7],digits=16) 

Since 10 times more than 6, rounds to 7 and adds 1.

A cleaner alternative for completing steps 0.1 is to use by :

 v=c() L<-0 for (a in seq(0, 4, by=0.1)){ for (b in seq(0, a, by=0.1)){ L<-L+1 v[L]<-b } } v 

Or maybe even cleaner:

 a <- 0:40 out <- list() for(i in seq(along=a)) out[[i]] <- 0:a[i] v <- unlist(out)/10 
+1
source

Source: https://habr.com/ru/post/1492313/


All Articles