Two interdepending for loops in R, strange results -


i try calculate in r different paramter values , b, parameter b should smaller or equal a. this, make 2 loops vary (from 0 4) , b 0 a, r gets me strange values of b.

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

it seems me b should run 0 in 0.1 steps. not always, steps smaller, can seen in positions 23-28 of vector v (for example). have idea why case. can't find mistake! thanks!

the documentation seq notes value of length.out rounded up. since a numeric , associated error, it's possible length of 1 more expect, gives weird output.

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" 

notice last line: 8 instead of 7.

to solve this, try converting length integer rounding:

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

Comments