0

I want to assign some value to a vecter like:

a = rep(0, 101)
for(i in seq(0, 1, 0.01)){
    u <- 100 * i + 1
    a[u] <- u
}
a
plot(a)

The output is

> a
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  30   0  31  32  33  34
 [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  59   0  60  61  62  63  64  65  66  67  68
 [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101

There are problems on the 29th and the 59th elements. They should be 29 and 59, but it turns out to be 0, the default value. And the previous values, the 28th and 58th, are also incorrect. Why is this happening? Thank you!

Carlton Chen
  • 615
  • 5
  • 9
  • if you try `u <- 1000 * i / 10 + 1` the problem solved, is this a serious rounding bug? – Carlton Chen Dec 05 '18 at 19:06
  • 1
    If you do `as.integer(c(0.29*100+1,0.029*1000+1,0.0029*10000+1,0.00029*100000+1))`, it returns either 29 or 30 depending on the case, but `as.integer(round(c(0.29*100+1,0.029*1000+1,0.0029*10000+1,0.00029*100000+1)))` will always return 30. It seems to be a problem related to integer conversion, that returns an imprecise number. Can't tell you more than that, unfortunately.. – Lamia Dec 05 '18 at 20:08
  • Have a look at these questions https://stackoverflow.com/questions/11128741/cast-variable-to-int-vs-round-function https://stackoverflow.com/questions/13204699/how-do-you-cast-a-double-to-an-integer-in-r – Lamia Dec 05 '18 at 20:11
  • Thank you@Lamia. I have no Computer Science background, but why such a confusing still not be well defined and solved? I think there should be absolutely no ambiguity in programming languages? – Carlton Chen Dec 05 '18 at 20:38
  • 1
    If you want to learn more, look up floating point arithmetic and precision. Also, have a look at circle one in this cool description of common problems and pitfalls in R https://www.burns-stat.com/pages/Tutor/R_inferno.pdf. – Lamia Dec 05 '18 at 23:04

1 Answers1

0

There is a problem with your indexing. I don't know how to explain why it doesn't work as written, but here is a modification to your code that works:

a = rep(0, 101)
s<-seq(0, 1, 0.01)
for(i in 1:101){
  a[i] <- 100 * s[i] + 1 
}
a
plot(a)

In general it is best to avoid multiple indexes in the same loop as it can be confusing and difficult to diagnose problems.

KamRa
  • 349
  • 2
  • 12
  • Thank you! But could you please give me some hint or keywords about the problem, I can learn it by myself. – Carlton Chen Dec 05 '18 at 19:31
  • To clarify (related to Lamia's comment), it likely has to do with the data types. Indexing should be done with integer data type because it is unambiguous. You are using a 'double' which is a floating-point data type in R. It is possible that the floating-point data type is causing the error in indexing. See this question for an example of some problems with floating-point inaccuracy: https://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples – KamRa Dec 05 '18 at 21:18