3

When programming within the R environment I used rep("[35,40)",1020). This should give me a list with 1020 times "[35,40)". However, the result contains only 1019 of these elements.

The programming was first done within a replicated for two vectors, but even when I split it up it doesn't work.

What I tried is using differen versions of R (R 2.11.1, R 2.9.0, R 2.10.0, R 2.7.2) but in none of them it works properly.

Has anyone an idea if there is a version of R which doesn't have this bug? Or how I could solve this problem?

So the code for this:

> agecats
 [1] "(-0.001,5]" "(5,10]"     "(10,15]"    "(15,20]"    "(20,25]"   
 [6] "(25,30]"    "(30,35]"    "(35,40]"    "(40,45]"    "(45,50]"   
[11] "(50,55]"    "(55,60]"    "(60,65]"    "(65,70]"    "(70,75]"   
[16] "(75,80]"  
> weightage<-c(0.9,0.9,2.7,3.1,8.9,10.05,10.05,10.2,10.2,9.3,9.3,8.7,7.9,3.15,3.15,1.5)
> weightage
 [1]  0.90  0.90  2.70  3.10  8.90 10.05 10.05 10.20 10.20  9.30  9.30  8.70
[13]  7.90  3.15  3.15  1.50

> weightage100<-weightage*100
> weightage100
 [1]   90   90  270  310  890 1005 1005 1020 1020  930  930  870  790  315  315
[16]  150
> tosamplefrom<-rep(agecats,weightage100)
> table(tosamplefrom)
tosamplefrom
(-0.001,5]    (10,15]    (15,20]    (20,25]    (25,30]    (30,35]    (35,40] 
        90        270        310        890       1005       1005       1019 
   (40,45]    (45,50]     (5,10]    (50,55]    (55,60]    (60,65]    (65,70] 
      1019        930         90        930        869        790        315 
   (70,75]    (75,80] 
       315        150 

And here I should have the 8 and 9 1020 times and it just gives 1019 times.

Kim

user404309
  • 211
  • 1
  • 6
  • 2
    I can't replicate this. What happens when you do `length(rep("[35,40)",1020))`? – Michael Dunn Jul 28 '10 at 09:20
  • Well, it works as you put it here. But the issue was that I coded I put 1020 in a variable name and the part to be replicated as well. And if I do it like that it gives as a result 1019. And on that I am not sure if there is some better way than to really do it by hand.. – user404309 Jul 28 '10 at 09:53
  • Could you please show us some code with the bug. – Michael Dunn Jul 28 '10 at 09:57
  • I added my code to the question.. – user404309 Jul 28 '10 at 10:10
  • that can't be your code… try again… also try pasting in your code exactly as run and pressing the little button at the top with 1's and 0's – John Jul 28 '10 at 10:28
  • I added the code again with your comment, John. Hope this is what you mean? At least this is what I did – user404309 Jul 28 '10 at 10:41
  • so you edited, made it more messy, and then made agecats 16 items long instead of 20. agecats isn't code, it's a result and putting it in like that makes it hard for someone to help you. You're not breaking lines, it all runs together. Making this neat in the first place might help you figure out what is wrong. Making it neat will definitely help someone else and make someone else more motivated. – John Jul 28 '10 at 10:46
  • I'm sorry for making it more messy. It was not totally clear to me how it would be clearer. Do you have any suggestions for it, so next time it will be properly? And thank you very much for the help! – user404309 Jul 28 '10 at 12:54

1 Answers1

11
(10.20 * 100) == 1020
FALSE

This is your problem. 10.2 can't be represented exactly and everything is going to hell because you're multiplying a floating point number and assuming it's an integer. It appears that R must be taking the floor of the number or just using as.integer for the conversion:

floor(10.2*100)
1019
as.integer(10.2*100)
1019

Rounding will work whether the floating point value is slightly above or slight below. The following change does fix the problem.

weightage100 <- round (weightage*100)

Read the R Inferno (floating point problems like this are not exclusive to R, for example, I just replicated it in python)

John
  • 23,360
  • 7
  • 57
  • 83