gpt4 book ai didi

R:为选定数量的值减去后续行

转载 作者:行者123 更新时间:2023-12-04 11:35:43 27 4
gpt4 key购买 nike

这是我目前拥有的数据:

df

patient ID Index_admission? adm_date dish_date
1244 FALSE 2/7/2009 2/8/2009
1244 TRUE 3/5/2009 3/15/2009
1244 FALSE 4/5/2011 4/7/2011
1244 FALSE 3/25/2012 3/27/2012
1244 TRUE 5/5/2012 5/20/2012
1244 TRUE 9/8/2013 9/15/2013
1244 FALSE 1/5/2014 1/15/2014
2333 FALSE 1/1/2010 1/8/2010
2333 FALSE 1/1/2011 1/5/2011
2333 TRUE 2/2/2011 2/25/2011
2333 FALSE 1/25/2012 1/28/2012
5422 TRUE 3/5/2015 3/15/2015
1243 TRUE 2/5/2009 2/8/2009
1243 TRUE 2/5/2011 2/19/2011

我需要从之前的 Index_admission 中找到 time_to_readmission。我需要添加一个新列,从正确的 dish_date 中减去 adm_date。仅当患者的 Index_admission 已达到 TRUE 时才应执行此操作。

另外如果患者有多个Index_admission,time_to_readmission 应始终计算为最接近的Index_admission 日期。

通过查看我希望数据的外观可能更容易解释:

df1

patient ID Index_admission? adm_date dish_date time_to_readmission
1244 FALSE 2/7/2009 2/8/2009 NA
1244 TRUE 3/5/2009 3/15/2009 NA
1244 FALSE 4/5/2011 4/7/2011 751
1244 FALSE 3/25/2012 3/27/2012 1106
1244 TRUE 5/5/2012 5/20/2012 1147
1244 TRUE 9/8/2013 9/15/2013 476
1244 FALSE 1/5/2014 1/15/2014 112
2333 FALSE 1/1/2010 1/8/2010 NA
2333 FALSE 1/1/2011 1/5/2011 NA
2333 TRUE 2/2/2011 2/25/2011 NA
2333 FALSE 1/25/2012 1/28/2012 334
5422 TRUE 3/5/2015 3/15/2015 NA
1243 TRUE 2/5/2009 2/8/2009 NA
1243 TRUE 2/5/2011 2/19/2011 727

请帮助我完成所需的编码。提前致谢。

> dput(df)
structure(list(patient.ID = c(124L, 124L, 124L, 124L, 124L, 124L,
124L, 233L, 233L, 233L, 233L, 542L, 1243L, 1243L), Index.admission. = c(FALSE,
TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE,
TRUE, TRUE, TRUE), adm_date = structure(c(8L, 10L, 12L, 9L, 13L,
14L, 4L, 1L, 2L, 5L, 3L, 11L, 6L, 7L), .Label = c("1/1/2010",
"1/1/2011", "1/25/2012", "1/5/2014", "2/2/2011", "2/5/2009",
"2/5/2011", "2/7/2009", "3/25/2012", "3/5/2009", "3/5/2015",
"4/5/2011", "5/5/2012", "9/8/2013"), class = "factor"), dish_date = structure(c(7L,
8L, 11L, 10L, 12L, 13L, 1L, 4L, 3L, 6L, 2L, 9L, 7L, 5L), .Label = c("1/15/2014",
"1/28/2012", "1/5/2011", "1/8/2010", "2/19/2011", "2/25/2011",
"2/8/2009", "3/15/2009", "3/15/2015", "3/27/2012", "4/7/2011",
"5/20/2012", "9/15/2013"), class = "factor")), .Names = c("patient.ID",
"Index.admission.", "adm_date", "dish_date"), class = "data.frame", row.names = c(NA,
-14L))

最佳答案

这应该有效。请注意,当我运行它时出现 data.table 类型错误,但答案是正确的。

这里需要注意的是,这会计算从满足您的条件的第一个dish_date 开始重新接纳的时间,这是您在帖子中要求的,“减去adm_date 来自 dish_date(前一行)”。你没有指定前一行......我选择第一个 dish_date 满足你的标准。

从您的示例输出来看,这并不是您正在做的。相反,您似乎对如何选择“前一行”有一些不清楚的标准。不清楚这条规则是什么。如果您想要不同的输出,请澄清问题

calc_readmit <- function(df) {
if (nrow(df) == 1) return(NA)
admitted <- c(0,cumsum(df$Index_admission))
admitted <- admitted[-length(admitted)]
dt1 <- df$dish_date[min(which(admitted > 0))-1]
admit2 <- ifelse(admitted > 0, dt1, NA)
time <- as.integer(df$adm_date) - admit2
as.integer(ifelse(admitted > 0, time, NA))
}

library(data.table)
df <- data.table(df, key= "id")
df <- df[, time_to_readmission := calc_readmit(.SD), by= "id"]

R> df
id Index_admission. adm_date dish_date time_to_readmission
1: 1243 TRUE 2009-02-05 2009-02-08 NA
2: 1243 TRUE 2011-02-05 2011-02-19 727
3: 1244 FALSE 2009-02-07 2009-02-08 NA
4: 1244 TRUE 2009-03-05 2009-03-15 NA
5: 1244 FALSE 2011-04-05 2011-04-07 751
6: 1244 FALSE 2012-03-25 2012-03-27 1106
7: 1244 TRUE 2012-05-05 2012-05-20 1147
8: 1244 TRUE 2013-09-08 2013-09-15 1638
9: 1244 FALSE 2014-01-05 2014-01-15 1757
10: 2333 FALSE 2010-01-01 2010-01-08 NA
11: 2333 FALSE 2011-01-01 2011-01-05 NA
12: 2333 TRUE 2011-02-02 2011-02-25 NA
13: 2333 FALSE 2012-01-25 2012-01-28 334
14: 5422 TRUE 2015-03-05 2015-03-15 NA

关于R:为选定数量的值减去后续行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913014/

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