gpt4 book ai didi

r - 将错误结果的数字分箱

转载 作者:行者123 更新时间:2023-12-02 04:26:28 25 4
gpt4 key购买 nike

我对数值向量进行 bin 后,输出出现问题。

我正在尝试对预先使用 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)#

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

当 right = FALSE 时,我没有得到 61.04 分类“>28 d”的 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 完成,那将是惊人的。但尊重我分配的垃圾箱。但是,我知道这还没有完成。因此,可以接受我提出的更正代码,但已更正。

最佳答案

cut 函数的 bins 是独占的。

来自 cut 函数的帮助:因子水平标签构造为“(b1, b2]”、“(b2, b3]”等。对于 right = TRUE 和“[b1, b2)”

为了包含最低值(或本例中的最高值),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)

关于r - 将错误结果的数字分箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074682/

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