patientcounter

fast, flexible census tables

Posted on April 4, 2020

Counting patients is hard

Blimey. It’s over 3 years since I collaborated with Neil Pettinger on some plots to demonstrate patient flow, using R. What started out as a supposed quick blog post morphed into several weeks of work, a blog post, workshops in Perth and Birmingham, and a much bigger audience than either of us had ever imagined.

One of the plots that Neil asked about was this one:

This is showing you how many patients are in a ward at any minute. That means counting who has been admitted prior to or during that interval, and who was discharged afterwards, or, not discharged at all.

It’s actually quite tricky, especially to start the plot, where you have to find everyone who was admitted prior to that period. It took me quite a while to figure out a method to do it, and it was highly specific.

A similar problem

A while back, I got asked to take a look at a spreadsheet by some colleagues in another department. They had the same challenge, they needed to know which patients were in hospital at a certain time each week. Only, it wasn’t just one hospital, and it was many patients.

The spreadsheet hogged my laptop memory like no other document before or since. Every time a filter got changed, hundreds of ‘COUNTIF’s started whirring away..I was able to come up with a SQL based solution, but if I’d been an ‘Excel only’ analyst, I’d have been stumped

Another problem

Slightly more recently, I got a call out of the blue from someone needing help installing R to run a Shiny app. We got that intalled no problem, but the app itself took about 10 minutes to become useable.
From delving into the code a bit, it appeared to be generating a 24 hour matrix for each day, and looping through patients to count whether they were in or not at each point.

How many times can I use the word ‘problem’?

I tried to find an alternative solution to this matrix / loop thing. I came across a dplyr row-wise thing, and it seemed to work. But then I saw that row-wise might be getting deprecated, and , long story, short, it was this problem that made me first realise that I needed to learn data.table

Solution

A solution that literally no one asked for, but I built it anyway.
It only has one function, but it’s flexible, and will churn out millions of rows in seconds, if your level of granularity requires it.

Although it is primarily aimed at healthcare users, anything that has a time in , and a time out, that might need to be counted (logistics / stock etc?) could be a potential use-case. In fact, if I’d thought about it more, and was aiming for world domination, I’d have called it something more generic. But, it’s done now, and I’m sticking with it.

There is a wee guide there on the package home page, it’s easy enough to use.

Oh , go on then. Here’s some code.

First, installation

install.packages("remotes")# if not already installedremotes::install_github("johnmackintosh/patientcounter")library(patientcounter)

Now, let’s take a look at the inbuilt dataset, which is as fake as a very fake thing indeed.

beds

You can see a typical dataset, but imagine in real life we have hundreds or thousands over a wide range. Most of these totally fake patients are only in for a day or two, whereas our real life data will be a lot more variable, and a lot harder to calculate.

Now, because you’re all clever, you’ll realise there are no counts here. If you set the results to ‘patient’ level, then you will get one row per patient per interval. This is very low level granularity, and you can use it to import into another (BI) tool of your choosing, to further aggregate and visualise as you see fit.

Alternatively, you may want to obtain a summary, grouped or otherwise.