gpt4 book ai didi

r - 怎么纠正cut方法的错误结果?

转载 作者:行者123 更新时间:2023-12-04 07:28:41 24 4
gpt4 key购买 nike

我试图获得 150 个样本的五个间隔,但显然 R cut 方法给了我错误的结果。下面我留下数据集和代码

age = c(89,81,59,80,74,36,28,73,44,51,31,75,31,60,42,16,38,76,72,67,47,31,63,45,58,78,45,90,41,53,24,47,41,24,59,
18,72,12,79,80,73,47,74,37,30,24,61,50,82,84,63,60,39,23,72,67,46,37,14,90,50,12,20,25,55,87,59,76,23,23,
36,61,19,65,42,80,23,41,20,77,20,52,54,33,38,53,71,78,71,12,77,40,80,23,45,58,73,24,50,15,81,61,59,33,37,
51,59,25,17,65,83,35,78,48,25,38,88,14,40,72,80,89,70,42,88,14,51,35,21,88,73,53,81,85,30,66,57,34,90,53,
40,80,76,24,33,69,81,52,35,33)

table((cut(age, breaks=5, right=FALSE, dig.lab=2))

# ---- results (wrong) ----
[12,28) [28,43) [43,59) [59,74) [74,90)
28 32 26 31 33
正确结果如下表所示


[12 - 28>
[28 - 43>
[43 - 59>
[59 - 74>
[74 - 90]


28
32
26
29
35

最佳答案

您的选择 dig.lab只影响级别的标签,而不影响切割的实际值。如果你跑

levels(cut(age, breaks=5, right=FALSE))
# [1] "[11.9,27.6)" "[27.6,43.2)" "[43.2,58.8)" "[58.8,74.4)"
# [5] "[74.4,90.1)"
你会看到实际的中断有小数点。即使您使用 dig.lab 更改标签,也会使用相同的中断。 . dig.lab选项纯粹是装饰性的。
如果您想使用这些确切的中断,您需要自己指定它们。例如
table(cut(age, breaks=c(12,38,43,59,74,90), right=FALSE, include.lowest = TRUE))
# [12,38) [38,43) [43,59) [59,74) [74,90]
# 47 13 26 29 35

关于r - 怎么纠正cut方法的错误结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68078167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com