以错误的结果对数字进行分箱

时间:2019-01-07 12:42:31

标签: r cut binning

将数值向量归类后,输出出现问题。

我正在尝试计算停留时间,该时间是使用difftime函数预先计算的。提供全部代码是没有意义的,因为这只是背景。但是,当我装箱时,我没有得到正确的答案。 这是分配给los的停留时间。

dput(los)
c(61.0416666666667, 61.0416666666667, 61.0416666666667, 2, 2, 3, 3)

这是我的休息时间。我在尝试几种方法时使用了na.rm。我以TRUE,FALSE通过了na.rm,使我脱离了休息。

breaks <- c(0, 0.8, 0.16, 
           1.0, 1.8, 1.16, 
           2.0, 2.8, 2.16, 
           3.0, 3.8, 3.16, 
           4.0, 4.8, 4.16, 
           5.0, 5.8, 5.16, 
           6.0, 6.8, 6.16, 
           7.0, 14.0, 21.0, 28.0, max(los)) #, , na.rm = FALSE

尽管如此,下一个代码还是尝试了

dt_los$losbinned <- cut(dt_los$LOS, 
                         breaks = breaks,
                         labels = c("0hrs", "8hrs", "16hrs", "1 d", 
                                 "1 d 8hrs", "1 d 16hrs", "2 d", 
                                 "2 d 8hrs", "2 d 16hrs", "3 d", 
                                 "3 d 8hrs", "3 d 16hrs", "4 d", 
                                 "4 d 8hrs", "4 d 16hrs", "5 d", 
                                 "5 d 8hrs", "5 d 16hrs", "6 d",  
                                 "6 d 8hrs","6 d 16hrs", "7 - 14 d", 
                                 "14 - 21 d", "21 - 28 d", "> 28 d"), 
                         right = FALSE)#

为“正确的”传递了不同的参数,这给了我:

当右= FALSE时,对于类别“> 28 d”,我未获得装箱的61.04的LOS。 B但是一定要为其他2.00和3.00分配正确的垃圾箱。

structure(list(IDcol = 101:107, Admissions = structure(c(1539160200, 
1539160200, 1539160200, 1539154800, 1539154800, 1539154800, 1539154800
), class = c("POSIXct", "POSIXt"), tzone = "Europe/London"), 
    Discharges = structure(c(1544434200, 1544434200, 1544434200, 
    1539327600, 1539327600, 1539414000, 1539414000), class = c("POSIXct", 
    "POSIXt"), tzone = "Europe/London"), Admission_type = c("Elective", 
    "Emergency", "Emergency", "Elective", "Emergency", "Elective", 
    "Emergency"), LOS = c(61.0416666666667, 61.0416666666667, 
    61.0416666666667, 2, 2, 3, 3), Ward_code = c("DSN", "DSN", 
    "DNA", "NAS", "BAS", "BAS", "BAS"), Same_day_discharge = c(FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), Spell_type = c("Elective", 
    "Emergency", "Emergency", "Elective", "Emergency", "Elective", 
    "Emergency"), Adm_period = c(TRUE, TRUE, TRUE, TRUE, TRUE, 
    TRUE, TRUE), losbinned = structure(c(NA, NA, NA, 7L, 7L, 
    10L, 10L), .Label = c("0hrs", "8hrs", "16hrs", "1 d", "1 d 8hrs", 
    "1 d 16hrs", "2 d", "2 d 8hrs", "2 d 16hrs", "3 d", "3 d 8hrs", 
    "3 d 16hrs", "4 d", "4 d 8hrs", "4 d 16hrs", "5 d", "5 d 8hrs", 
    "5 d 16hrs", "6 d", "6 d 8hrs", "6 d 16hrs", "7 - 14 d", 
    "14 - 21 d", "21 - 28 d", "> 28 d"), class = "factor")), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))

当我通过right = TRUE时,61.04的输出将合并到“> 28 d”,这是所需的答案,但是,我没有得到2.0和3.0的正确容器,它们在1 d 16小时内被合并2.0和2 d 16小时,共3个。同样,它们应分别装在2个3中。

 structure(list(IDcol = 101:107, Admissions = structure(c(1539160200, 
1539160200, 1539160200, 1539154800, 1539154800, 1539154800, 1539154800
), class = c("POSIXct", "POSIXt"), tzone = "Europe/London"), 
    Discharges = structure(c(1544434200, 1544434200, 1544434200, 
    1539327600, 1539327600, 1539414000, 1539414000), class = c("POSIXct", 
    "POSIXt"), tzone = "Europe/London"), Admission_type = c("Elective", 
    "Emergency", "Emergency", "Elective", "Emergency", "Elective", 
    "Emergency"), LOS = c(61.0416666666667, 61.0416666666667, 
    61.0416666666667, 2, 2, 3, 3), Ward_code = c("DSN", "DSN", 
    "DNA", "NAS", "BAS", "BAS", "BAS"), Same_day_discharge = c(FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), Spell_type = c("Elective", 
    "Emergency", "Emergency", "Elective", "Emergency", "Elective", 
    "Emergency"), Adm_period = c(TRUE, TRUE, TRUE, TRUE, TRUE, 
    TRUE, TRUE), losbinned = structure(c(25L, 25L, 25L, 6L, 6L, 
    9L, 9L), .Label = c("0hrs", "8hrs", "16hrs", "1 d", "1 d 8hrs", 
    "1 d 16hrs", "2 d", "2 d 8hrs", "2 d 16hrs", "3 d", "3 d 8hrs", 
    "3 d 16hrs", "4 d", "4 d 8hrs", "4 d 16hrs", "5 d", "5 d 8hrs", 
    "5 d 16hrs", "6 d", "6 d 8hrs", "6 d 16hrs", "7 - 14 d", 
    "14 - 21 d", "21 - 28 d", "> 28 d"), class = "factor")), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))

实际结果和预期结果应该是为我的逗留时间分配的正确垃圾箱。对于61.04->“> 28d”,对于2->“ 2 d”,对于3->“ 3 d”。

如果可以使用tidyverse做到这一点,那就太好了。但是尊重我分配的垃圾箱。但是,我知道这还没有完成。因此,可以使用我提出但已更正的更正代码。

1 个答案:

答案 0 :(得分:0)

剪切功能的容器是独占的。

在剪切功能的帮助下:对于右= TRUE,因子级别标签被构造为“(b1,b2]”,“(b2,b3]”等,并且被构造为“ [b1,b2)” < / em>

为了包括最小值(在这种情况下为最大值),必须使用include.lowest=TRUE选项。这将使第一个bin独占到独占“ [b1,b2]”。

尝试:

labels<-c("0hrs", "8hrs", "16hrs", "1 d", 
          "1 d 8hrs", "1 d 16hrs", "2 d", 
          "2 d 8hrs", "2 d 16hrs", "3 d", 
          "3 d 8hrs", "3 d 16hrs", "4 d", 
          "4 d 8hrs", "4 d 16hrs", "5 d", 
          "5 d 8hrs", "5 d 16hrs", "6 d",  
          "6 d 8hrs","6 d 16hrs", "7 - 14 d", 
          "14 - 21 d", "21 - 28 d", "> 28 d")

dt_los$losbinned <- cut(los, breaks=breaks, labels=labels, right=FALSE, include.lowest = TRUE)