从日期和时间计算医院的入住率。 [英] Calculating Occupancy in hospital from dates with time.

查看:361
本文介绍了从日期和时间计算医院的入住率。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过tidyverse计算急诊室(ED)的占用率。在这个特定的问题中,入住率被理解为入院,但在入院的同一小时内没有离开医院。一个更清楚的例子是:如果我在12:00:00到达ED,并且在我入院的那一小时内没有离开,那么我就是在医院。因此,为此,我需要创建一个新的列占用率。 (提供一点见解-我想按一天中的小时数绘制占用率。但是我知道如何绘制该占用率,但不知道如何计算占用率。因此,您无需像我这样就陷入困境让您了解我的项目)。不过,我需要的是学习如何从下面的表格中计算入住率。请帮忙。

I am looking to calculate occupancy in emergency department (ED) with tidyverse. Occupancy is understood here in this particular problem as Admitted but did not leave the hospital within the same hour they were admitted. A clearer example would be: if I came at ED at 12:00:00 and did not leave within the hour I was admitted, then I am occupying the bospital. So for this I need to create a new column Occupancy. (A little insight to give - I want to plot occupancy by hour of the day. Yet I know how to plot this, but do not know how to calculate occupancy. Thus no need for you to be bogged down on this issue as I am giving you an insight of my project). What I need though is to learn how to calculate occupancy from the table I have bellow. Please do help.

我有ID,入场券= Adm,碟片=放电。

I have ID, Admission = Adm and Disc = Discharges.

ID = c(101, 102,103, 104, 105, 106, 107)

Adm = as.POSIXct(c("2012-01-12 00:52:00", "2012-01-12 00:55:00", "2012-02-12 
                    01:35:00", "2012-02-12 03:24:00", "2012-02-12 04:24:00", 
                   "2012-02-12 05:24:00", "2012-02-12 05:28:00"))

Disc = as.POSIXct(c("2012-01-12 02:00:00", "2012-01-12 02:59:00", "2012-01-12 
                     03:01:00", "2012-01-12 05:01:00", "2012-01-12 06:01:00", 
                    "2012-01-12 08:01:00", "2012-01-12 08:01:00"))

df = data.frame(ID, Adm, Disc)

我已经从入场时间中提取了小时。这样我就可以使用新列来计算占用率了-在眼前这个问题理解为已入院,但在患者入院的一小时内没有出院。提醒您,我想使用tidyverse库完成此操作

I have extracted the hour from the Admission. So that I can use the new column for calculating the occupancy - understood at the problem at hand as Admitted but were not discharged within the hour the patients were admitted. To remind you, I want to do this with tidyverse library

df_hour <- df %>%
  mutate(Hour_Adm = lubridate::hour(as.POSIXct(Adm, "%Y%m%d %H:%M:%S"))) 

非常感谢任何帮助。谢谢。

Any help is very much appreciated. Thank you.

推荐答案

逻辑是增加1小时(即 60 * 60 秒)到 Adm 时间(属于 POSIXct 类型),并将其与 Disc 时间。

Logic is to add 1 hour (i.e. 60*60 seconds) to Adm time (which is of POSIXct type) and compare it with Disc time.

第一&对于其中多个行具有 ID 的情况,添加了 last 。那么最早的 Adm 和最新的 Disc 时间将仅根据每个 ID

First & last is added for cases wherein multiple rows are there for an ID. Then the earliest Adm and latest Disc time will only be considered per ID.




library(tidyverse)

df %>%
  group_by(ID) %>%
  mutate(occupancy = ifelse(last(Disc) > first(Adm) + 60*60, 1, 0))

这样

     ID Adm                 Disc                occupancy
  <dbl> <dttm>              <dttm>                  <dbl>
1   101 2012-01-12 00:52:00 2012-01-12 02:00:00      1.00
2   102 2012-01-12 00:55:00 2012-01-12 02:59:00      1.00
3   103 2012-02-12 01:35:00 2012-01-12 03:01:00      0   
4   104 2012-02-12 03:24:00 2012-01-12 05:01:00      0   
5   105 2012-02-12 04:24:00 2012-01-12 06:01:00      0   
6   106 2012-02-12 05:24:00 2012-01-12 08:01:00      0   
7   107 2012-02-12 05:28:00 2012-01-12 08:01:00      0  



样本数据:

df <- structure(list(ID = c(101, 102, 103, 104, 105, 106, 107), Adm = structure(c(1326309720, 
1326309900, 1328990700, 1328997240, 1329000840, 1329004440, 1329004680
), class = c("POSIXct", "POSIXt"), tzone = ""), Disc = structure(c(1326313800, 
1326317340, 1326317460, 1326324660, 1326328260, 1326335460, 1326335460
), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("ID", 
"Adm", "Disc"), row.names = c(NA, -7L), class = "data.frame")

这篇关于从日期和时间计算医院的入住率。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆