R:当使用rep(..,..)复制1020个a字符变量时,结果只包含1019个重复?

发布于 2024-09-11 09:03:28 字数 1385 浏览 4 评论 0原文

在 R 环境中编程时,我使用了rep("[35,40)",1020)。这应该给我一个包含 1020 次“[35,40)”的列表。但是,结果仅包含这些元素中的 1019 个。

编程首先是在两个向量的复制中完成的,但即使我将其分开,它也不起作用。

我尝试使用不同版本的 R(R 2.11.1、R 2.9.0、R 2.10.0、R 2.7.2),但在它们中都无法正常工作。

有谁知道是否有 R 版本没有这个错误?或者我该如何解决这个问题?

所以代码如下:

> 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 

这里我应该有 8 和 9 1020 次,但它只给出 1019 次。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-09-18 09:03:28
(10.20 * 100) == 1020
FALSE

这是你的问题。 10.2 无法准确表示,一切都会变得很糟糕,因为你要乘以浮点数并假设它是整数。看来 R 必须取数字的下限或仅使用 as.integer 进行转换:

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

无论浮点值略高于还是略低于,舍入都会起作用。以下更改确实解决了该问题。

weightage100 <- round (weightage*100)

阅读R Inferno(像这样的浮点问题并不限于以R为例,我只是在python中复制了它)

(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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文