温馨提示:本文翻译自stackoverflow.com,查看原文请点击:label - Is there a way to write these multiple break points (with equal step length) in R function cut more
r label cut

label - 有没有办法在R函数中写入这些多个断点(具有相等的步长),更多

发布于 2020-03-30 21:36:06

这就是我所做的,它可以达到我想要的结果,但是效率很低。

cut(df1$wage, breaks = c(-Inf, 20000,21000,22000,23000,24000,25000,26000,27000,28000,29000,30000, Inf), 
         include.lowest=TRUE, dig.lab=10, labels = c("-20 000", "20 000-21 000", "21 000-22 000", "22 000-23 000", "23 000-24 000",
                                                    "24 000-25 000", "25 000-26 000", "26 000-27 000", "27 000-28 000", "28 000-29 000", "29 000-30 000", "30 000-"))

我想要一个最低的bin,其中包括直到某个指定值(例如20000)的所有值。对于所有高于30 000的值都相同。

而且,我希望能够更改断点之间的步长,在示例中,断点现在为1000,也就是说为500,而不必明确指定所有断点。

理想情况下,我还希望标签遵循我指定的断点,否则这将成为效率很低的过程

对于休息部分,我很接近,breaks = (seq(from = 20000, to = 30000, by = 1000))但无法像上面的示例一样想出如何也包括底部和顶部垃圾箱

查看更多

提问者
Åskan
被浏览
15
Ronak Shah 2020-01-31 18:09

您可以将中断存储在向量中并在breaks和中使用labels

breaks <- seq(from = 20000, to = 30000, by = 1000)

cut(df1$wage, breaks = c(-Inf, breaks Inf), include.lowest=TRUE, dig.lab=10, 
 labels = c(-20000, paste(head(breaks, -1), tail(breaks, -1), sep = "-"), "30000-"))