1

I was trying to run this simple code in R and that's what I got:

table <- matrix(NA, nrow=10, ncol=1)
for (i in seq(0.01, 0.10, by=0.01)) {
  table[i*100, ] <- i
}
table
#      [,1]
# [1,] 0.01
# [2,] 0.02
# [3,] 0.03
# [4,] 0.04
# [5,] 0.05
# [6,] 0.07
# [7,]   NA
# [8,] 0.08
# [9,] 0.09
#[10,] 0.10

If I run my code backwards, it works fine:

table <- matrix(NA, nrow=10, ncol=1)
for (i in seq(0.1, 0.01, by=-0.01)) {
  table[i*100, ] <- i
}
table
#      [,1]
# [1,] 0.01
# [2,] 0.02
# [3,] 0.03
# [4,] 0.04
# [5,] 0.05
# [6,] 0.06
# [7,] 0.07
# [8,] 0.08
# [9,] 0.09
#[10,] 0.10

Does anyone know what's going on?

I'm using R version 3.2.0 on Mac.

Katherine
  • 48
  • 4
  • 1
    Check the result of `all.equal(seq(0.01, 0.10, by=0.01)[7], 7)`. As well as http://stackoverflow.com/a/9508558/3710546 –  Jun 03 '15 at 05:56
  • The result is "Mean relative difference: 99". I don't understand what it has to do with my issue. Can you please explain? – Katherine Jun 03 '15 at 06:05
  • It means that 0.07*100 is not equal to 7, from the floating point viewpoint: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f –  Jun 03 '15 at 06:11
  • This is a known issue in computer science. Consider creating the table in a different way. `matrix(seq(0.01, 0.10, by=0.01), nrow=10, ncol=1)` – Pierre L Jun 03 '15 at 06:13
  • Thanks! But then why does it write to row 6? Shouldn't it round and write to row 7? – Katherine Jun 03 '15 at 06:16
  • Please read the links. –  Jun 03 '15 at 06:18
  • @plafort I'm actually running a different code and I need to fill my table in a for-loop. Thanks anyways! I know how to correct the issue. – Katherine Jun 03 '15 at 06:19
  • If you round the result (`round(seq(0.01, 0.1, by = 0.01) * 100, 1)`) you should be fine. – Roman Luštrik Jun 03 '15 at 06:22

1 Answers1

1

This has to do with rounding. You are not adressing the same components of the vector as you see with more digits and the multiplication by 100:

    options(digits = 22)
     > seq(0.01, 0.10, by=0.01)*100
     [1]  1.000000000000000000000  2.000000000000000000000  3.000000000000000000000  4.000000000000000000000  5.000000000000000000000  6.000000000000000888178  6.999999999999999111822  8.000000000000000000000  9.000000000000000000000 10.000000000000000000000
     > seq(0.1, 0.01, by=-0.01)*100
     [1] 10.000000000000000000000  9.000000000000001776357  8.000000000000000000000  7.000000000000000888178  6.000000000000000888178  5.000000000000000000000  4.000000000000000888178  3.000000000000000000000  2.000000000000000444089  1.000000000000000888178
HOSS_JFL
  • 765
  • 2
  • 9
  • 24
  • Why does it happen only with 7 in the ascending order? – Katherine Jun 03 '15 at 06:08
  • as you see only the integer part of the numbers are used for the indexing of the vector. 6.999999999999999111822 equals 6 in this sense here. You need to avoid this kind of indexing with decimal numbers. – HOSS_JFL Jun 03 '15 at 06:13
  • I don't think that's what @user4968068 is asking. He or she is interested to know why it works when indexing backwards. I guess this is the internal working of the function, but I'm not familiar enough with the function to dig out the details. – Roman Luštrik Jun 03 '15 at 06:22