Counts the number of patients in each location, by the specified interval, for the duration of the patient admission. Results can be returned as a grand totals, grouped totals, or individual patient level per interval.

interval_census(
  df,
  identifier,
  admit,
  discharge,
  group_var = NULL,
  time_unit = "1 hour",
  time_adjust_period = NULL,
  time_adjust_value = NULL,
  results = c("patient", "group", "total"),
  uniques = TRUE
)

Arguments

df

dataframe, tibble or data.table.

identifier

Unique patient identifier.

admit

Datetime of admission as POSIXct.

discharge

Datetime of discharge as POSIXct.

group_var

Optional unique character vector to identify specific patient location or responsible clinician at each interval, or at time of a change in location / responsible clinician during the interval.

time_unit

Character string to denote time intervals to count by e.g. "1 hour", "15 mins".

time_adjust_period

Optional argument which allows the user to obtain a snapshot at a specific time of day by making slight adjustments to the specified interval. Possible values are "start_sec","start_min","end_sec", or "end_min". For example, you may specify hourly intervals, but adjust these to 1 minute past the hour with "start_min", or several seconds before the end with "end_sec".

time_adjust_value

Optional. An integer to adjust the startor end of each period in minutes or seconds, depending on the chosen time_adjust_period (if specified).

results

A character string specifying the granularity of the results.

'patient' returns one row per patient, group_var and interval. The results can be input to external tools for further analysis or visualisation.

'group' provides an overall grouped count of patients by the specified time interval.

'total' returns the grand total of patients by each unique time interval.

uniques

Logical. Specifies how to deal with patients who move during an interval, and subsequently have two or more records per interval. Set "uniques" to TRUE to get a distinct count of patients per interval. To be clear, TRUE will count patients only once per interval.

Setting "uniques" to FALSE will count each patient entry per interval. If a patient moves during an interval then at least two rows will be returned for tha patient for that particular interval. This is useful if you want to count occupied beds, or track moves or transfers between departments.

In general, if you use a grouping variable, set "uniques" to FALSE.

Value

data.table showing the patient identifier, the specified group variable , and the count by the relevant unit of time. Also included are the start and end of the interval, plus the date and base hour for convenient interactive filtering of the results.

Examples

# \donttest{ interval_census(beds, identifier ="patient", admit = "start_time", discharge = "end_time", time_unit = "1 hour", results = "total") interval_census(beds, identifier ="patient", admit = "start_time", discharge = "end_time", time_unit = "1 hour", results = "patient")
#> bed patient start_time end_time interval_beginning #> 1: A 1 2020-01-01 09:34:00 2020-01-01 10:34:00 2020-01-01 09:00:00 #> 2: B 5 2020-01-01 09:45:00 2020-01-01 14:45:00 2020-01-01 09:00:00 #> 3: A 1 2020-01-01 09:34:00 2020-01-01 10:34:00 2020-01-01 10:00:00 #> 4: B 5 2020-01-01 09:45:00 2020-01-01 14:45:00 2020-01-01 10:00:00 #> 5: C 9 2020-01-01 10:05:00 2020-01-01 10:35:00 2020-01-01 10:00:00 #> --- #> 116: A 4 2020-01-01 18:00:00 2020-01-03 00:00:00 2020-01-02 21:00:00 #> 117: D 10 2020-01-01 10:30:00 2020-01-03 00:00:00 2020-01-02 22:00:00 #> 118: A 4 2020-01-01 18:00:00 2020-01-03 00:00:00 2020-01-02 22:00:00 #> 119: D 10 2020-01-01 10:30:00 2020-01-03 00:00:00 2020-01-02 23:00:00 #> 120: A 4 2020-01-01 18:00:00 2020-01-03 00:00:00 2020-01-02 23:00:00 #> interval_end base_date base_hour #> 1: 2020-01-01 10:00:00 2020-01-01 9 #> 2: 2020-01-01 10:00:00 2020-01-01 9 #> 3: 2020-01-01 11:00:00 2020-01-01 10 #> 4: 2020-01-01 11:00:00 2020-01-01 10 #> 5: 2020-01-01 11:00:00 2020-01-01 10 #> --- #> 116: 2020-01-02 22:00:00 2020-01-02 21 #> 117: 2020-01-02 23:00:00 2020-01-02 22 #> 118: 2020-01-02 23:00:00 2020-01-02 22 #> 119: 2020-01-03 00:00:00 2020-01-02 23 #> 120: 2020-01-03 00:00:00 2020-01-02 23
interval_census(beds, identifier ="patient", admit = "start_time", discharge = "end_time", group_var = "bed", time_unit = "1 hour", results = "group", uniques = FALSE)
#> bed interval_beginning interval_end base_date base_hour N #> 1: A 2020-01-01 09:00:00 2020-01-01 10:00:00 2020-01-01 9 1 #> 2: B 2020-01-01 09:00:00 2020-01-01 10:00:00 2020-01-01 9 1 #> 3: A 2020-01-01 10:00:00 2020-01-01 11:00:00 2020-01-01 10 2 #> 4: B 2020-01-01 10:00:00 2020-01-01 11:00:00 2020-01-01 10 1 #> 5: C 2020-01-01 10:00:00 2020-01-01 11:00:00 2020-01-01 10 1 #> 6: D 2020-01-01 10:00:00 2020-01-01 11:00:00 2020-01-01 10 1 #> 7: B 2020-01-01 11:00:00 2020-01-01 12:00:00 2020-01-01 11 1 #> 8: A 2020-01-01 11:00:00 2020-01-01 12:00:00 2020-01-01 11 2 #> 9: D 2020-01-01 11:00:00 2020-01-01 12:00:00 2020-01-01 11 1 #> 10: B 2020-01-01 12:00:00 2020-01-01 13:00:00 2020-01-01 12 1 #> 11: D 2020-01-01 12:00:00 2020-01-01 13:00:00 2020-01-01 12 1 #> 12: A 2020-01-01 12:00:00 2020-01-01 13:00:00 2020-01-01 12 1 #> 13: B 2020-01-01 13:00:00 2020-01-01 14:00:00 2020-01-01 13 1 #> 14: D 2020-01-01 13:00:00 2020-01-01 14:00:00 2020-01-01 13 1 #> 15: A 2020-01-01 13:00:00 2020-01-01 14:00:00 2020-01-01 13 1 #> 16: B 2020-01-01 14:00:00 2020-01-01 15:00:00 2020-01-01 14 1 #> 17: D 2020-01-01 14:00:00 2020-01-01 15:00:00 2020-01-01 14 1 #> 18: A 2020-01-01 14:00:00 2020-01-01 15:00:00 2020-01-01 14 1 #> 19: D 2020-01-01 15:00:00 2020-01-01 16:00:00 2020-01-01 15 1 #> 20: A 2020-01-01 15:00:00 2020-01-01 16:00:00 2020-01-01 15 1 #> 21: D 2020-01-01 16:00:00 2020-01-01 17:00:00 2020-01-01 16 1 #> 22: A 2020-01-01 16:00:00 2020-01-01 17:00:00 2020-01-01 16 1 #> 23: B 2020-01-01 16:00:00 2020-01-01 17:00:00 2020-01-01 16 1 #> 24: D 2020-01-01 17:00:00 2020-01-01 18:00:00 2020-01-01 17 1 #> 25: A 2020-01-01 17:00:00 2020-01-01 18:00:00 2020-01-01 17 1 #> 26: B 2020-01-01 17:00:00 2020-01-01 18:00:00 2020-01-01 17 1 #> 27: D 2020-01-01 18:00:00 2020-01-01 19:00:00 2020-01-01 18 1 #> 28: A 2020-01-01 18:00:00 2020-01-01 19:00:00 2020-01-01 18 2 #> 29: B 2020-01-01 18:00:00 2020-01-01 19:00:00 2020-01-01 18 1 #> 30: D 2020-01-01 19:00:00 2020-01-01 20:00:00 2020-01-01 19 1 #> 31: A 2020-01-01 19:00:00 2020-01-01 20:00:00 2020-01-01 19 2 #> 32: B 2020-01-01 19:00:00 2020-01-01 20:00:00 2020-01-01 19 1 #> 33: D 2020-01-01 20:00:00 2020-01-01 21:00:00 2020-01-01 20 1 #> 34: A 2020-01-01 20:00:00 2020-01-01 21:00:00 2020-01-01 20 2 #> 35: B 2020-01-01 20:00:00 2020-01-01 21:00:00 2020-01-01 20 1 #> 36: D 2020-01-01 21:00:00 2020-01-01 22:00:00 2020-01-01 21 1 #> 37: A 2020-01-01 21:00:00 2020-01-01 22:00:00 2020-01-01 21 2 #> 38: B 2020-01-01 21:00:00 2020-01-01 22:00:00 2020-01-01 21 2 #> 39: D 2020-01-01 22:00:00 2020-01-01 23:00:00 2020-01-01 22 1 #> 40: A 2020-01-01 22:00:00 2020-01-01 23:00:00 2020-01-01 22 2 #> 41: B 2020-01-01 22:00:00 2020-01-01 23:00:00 2020-01-01 22 1 #> 42: D 2020-01-01 23:00:00 2020-01-02 00:00:00 2020-01-01 23 1 #> 43: A 2020-01-01 23:00:00 2020-01-02 00:00:00 2020-01-01 23 2 #> 44: B 2020-01-01 23:00:00 2020-01-02 00:00:00 2020-01-01 23 1 #> 45: D 2020-01-02 00:00:00 2020-01-02 01:00:00 2020-01-02 0 1 #> 46: A 2020-01-02 00:00:00 2020-01-02 01:00:00 2020-01-02 0 2 #> 47: B 2020-01-02 00:00:00 2020-01-02 01:00:00 2020-01-02 0 1 #> 48: D 2020-01-02 01:00:00 2020-01-02 02:00:00 2020-01-02 1 1 #> 49: A 2020-01-02 01:00:00 2020-01-02 02:00:00 2020-01-02 1 2 #> 50: D 2020-01-02 02:00:00 2020-01-02 03:00:00 2020-01-02 2 1 #> 51: A 2020-01-02 02:00:00 2020-01-02 03:00:00 2020-01-02 2 2 #> 52: D 2020-01-02 03:00:00 2020-01-02 04:00:00 2020-01-02 3 1 #> 53: A 2020-01-02 03:00:00 2020-01-02 04:00:00 2020-01-02 3 2 #> 54: D 2020-01-02 04:00:00 2020-01-02 05:00:00 2020-01-02 4 1 #> 55: A 2020-01-02 04:00:00 2020-01-02 05:00:00 2020-01-02 4 2 #> 56: D 2020-01-02 05:00:00 2020-01-02 06:00:00 2020-01-02 5 1 #> 57: A 2020-01-02 05:00:00 2020-01-02 06:00:00 2020-01-02 5 2 #> 58: D 2020-01-02 06:00:00 2020-01-02 07:00:00 2020-01-02 6 1 #> 59: A 2020-01-02 06:00:00 2020-01-02 07:00:00 2020-01-02 6 2 #> 60: D 2020-01-02 07:00:00 2020-01-02 08:00:00 2020-01-02 7 1 #> 61: A 2020-01-02 07:00:00 2020-01-02 08:00:00 2020-01-02 7 2 #> 62: D 2020-01-02 08:00:00 2020-01-02 09:00:00 2020-01-02 8 1 #> 63: A 2020-01-02 08:00:00 2020-01-02 09:00:00 2020-01-02 8 2 #> 64: D 2020-01-02 09:00:00 2020-01-02 10:00:00 2020-01-02 9 1 #> 65: A 2020-01-02 09:00:00 2020-01-02 10:00:00 2020-01-02 9 2 #> 66: D 2020-01-02 10:00:00 2020-01-02 11:00:00 2020-01-02 10 1 #> 67: A 2020-01-02 10:00:00 2020-01-02 11:00:00 2020-01-02 10 2 #> 68: D 2020-01-02 11:00:00 2020-01-02 12:00:00 2020-01-02 11 1 #> 69: A 2020-01-02 11:00:00 2020-01-02 12:00:00 2020-01-02 11 2 #> 70: D 2020-01-02 12:00:00 2020-01-02 13:00:00 2020-01-02 12 1 #> 71: A 2020-01-02 12:00:00 2020-01-02 13:00:00 2020-01-02 12 2 #> 72: D 2020-01-02 13:00:00 2020-01-02 14:00:00 2020-01-02 13 1 #> 73: A 2020-01-02 13:00:00 2020-01-02 14:00:00 2020-01-02 13 2 #> 74: D 2020-01-02 14:00:00 2020-01-02 15:00:00 2020-01-02 14 1 #> 75: A 2020-01-02 14:00:00 2020-01-02 15:00:00 2020-01-02 14 2 #> 76: D 2020-01-02 15:00:00 2020-01-02 16:00:00 2020-01-02 15 1 #> 77: A 2020-01-02 15:00:00 2020-01-02 16:00:00 2020-01-02 15 2 #> 78: D 2020-01-02 16:00:00 2020-01-02 17:00:00 2020-01-02 16 1 #> 79: A 2020-01-02 16:00:00 2020-01-02 17:00:00 2020-01-02 16 2 #> 80: D 2020-01-02 17:00:00 2020-01-02 18:00:00 2020-01-02 17 1 #> 81: A 2020-01-02 17:00:00 2020-01-02 18:00:00 2020-01-02 17 2 #> 82: D 2020-01-02 18:00:00 2020-01-02 19:00:00 2020-01-02 18 1 #> 83: A 2020-01-02 18:00:00 2020-01-02 19:00:00 2020-01-02 18 1 #> 84: D 2020-01-02 19:00:00 2020-01-02 20:00:00 2020-01-02 19 1 #> 85: A 2020-01-02 19:00:00 2020-01-02 20:00:00 2020-01-02 19 1 #> 86: D 2020-01-02 20:00:00 2020-01-02 21:00:00 2020-01-02 20 1 #> 87: A 2020-01-02 20:00:00 2020-01-02 21:00:00 2020-01-02 20 1 #> 88: D 2020-01-02 21:00:00 2020-01-02 22:00:00 2020-01-02 21 1 #> 89: A 2020-01-02 21:00:00 2020-01-02 22:00:00 2020-01-02 21 1 #> 90: D 2020-01-02 22:00:00 2020-01-02 23:00:00 2020-01-02 22 1 #> 91: A 2020-01-02 22:00:00 2020-01-02 23:00:00 2020-01-02 22 1 #> 92: D 2020-01-02 23:00:00 2020-01-03 00:00:00 2020-01-02 23 1 #> 93: A 2020-01-02 23:00:00 2020-01-03 00:00:00 2020-01-02 23 1 #> bed interval_beginning interval_end base_date base_hour N
# }