R从时间变量获得停留的数量和长度

时间:2016-06-05 13:31:57

标签: r

我有兴趣了解医院每位住院病人的住院时间和住院时间。困难在于患者可以每年住院几次(有休息时间)作为住院患者,如例子中的患者16。

以下是我的数据的最小示例:

PID              Time Inpatient
1 14-01-12 21:37:00         1
1 14-01-12 21:40:00         1
1 14-01-13 07:23:00         1
1 14-01-13 07:23:00         1
2 14-01-27 12:36:00         0
3 14-02-11 10:06:00         0
3 14-08-04 12:43:00         0
16 14-10-14 17:16:00         1
16 14-10-15 06:41:00         1
16 14-10-15 11:15:00         1
16 14-10-16 17:51:00         1
16 14-10-16 18:47:00         1
16 14-10-17 11:22:00         1
16 14-10-17 18:41:00         1
16 14-10-18 11:40:00         1
16 14-12-18 08:57:00         1
16 14-12-18 09:10:00         1
16 14-12-18 12:43:00         1

或:

PID <- c(rep(1, 4), 2, 3, 3, rep(16, 11))
Time <- c("14-01-12 21:37:00 LMT", "14-01-12 21:40:00 LMT", "14-01-13 07:23:00 LMT",
          "14-01-13 07:23:00 LMT", "14-01-27 12:36:00 LMT", "14-02-11 10:06:00 LMT",
          "14-08-04 12:43:00 LMT", "14-10-14 17:16:00 LMT", "14-10-15 06:41:00 LMT", 
          "14-10-15 11:15:00 LMT", "14-10-16 17:51:00 LMT", "14-10-16 18:47:00 LMT",
          "14-10-17 11:22:00 LMT", "14-10-17 18:41:00 LMT", "14-10-18 11:40:00 LMT",
          "14-12-18 08:57:00 LMT", "14-12-18 09:10:00 LMT", "14-12-18 12:43:00 LMT")
Inpatient <- c(rep(1, 4), rep(0, 3), rep(1, 11))
df <- data.frame(PID = PID, Time = Time, Inpatient = Inpatient)

我想要的是一个看起来像这样的列:

df$Stay <- c(rep(1, 4), rep(0, 3), rep(1, 8), rep(2, 3))

从这一栏开始,我想计算每次逗留的持续时间。

感谢任何帮助。谢谢!

0 个答案:

没有答案