Count ID-numbers over time

Hi there,
I have the following table:
  • PID // Datum
  • 23 //'21-12-2017'
  • 23 //'28-12-2017'
  • 25 //'10-1-2018'
  • 26 //'11-1-2018'
  • 1 //'15-1-2018'
  • 25 //'17-1-2018'
  • 10 //'17-1-2018'
  • 21 //'17-1-2018'
  • 7 //'17-1-2018'
  • 11 //'17-1-2018'
I need to determine the amount of patients over time (PID = Patient ID). So in fact a function, which gives the following table:
  • Patients // Datum
  • 1 //'21-12-2017'
  • 2 //'10-01-2018'
  • 3 //'11-01-2018'etc.
Is there anyway to accomplish this? Many thanks.

 Accepted Answer

[Datum, ~, dateidx] = unique(T.Datum);
Patients = accumarray(dateidx(:), 1);
output = table(Datum, Patients);

5 Comments

Chien Nguyen
Chien Nguyen on 25 Jun 2018
Edited: Chien Nguyen on 25 Jun 2018
Thanks for your quick reply!
This function gives the following output:
  • Datum // Patients
  • '1-2-2018' // 7
  • '1-5-2018' // 5
  • '10-1-2018' // 2
So it doesn't work yet. I think it is finding unique datetime elements instead of unique PID numbers. Do you know the solution?
output = table(Patients, Datum);
In the example you gave, I cannot see how you determined the Patients count.
When you talk about the amount of patients over time, it is not clear whether you want to ask about the number of total number of visits per day, or the total number of different patients per day (so if the same person visited multiple times on a day they would be counted only once), or the total number of different patients that have been seen since the beginning of the record keeping.
Or perhaps you want to know the first date on which any particular patient visited, except that you want to anonymize the PID sequentially, so the entry with label 1 refers to the first unique patient by PID? This is the only interpretation I can see that is consistent with your example output.
I'm looking for the total count of different patients visited over time. So if for instance, patient 1 visits the hospital on 21-12-2017 and aganin on 28-12-2017. Patient 2 visits the hospital on 12-01-2018.
This will result in 2 participants on 10-01-2018, which will be presented in this table:
  • Patients // Datum
  • 1 // 21-12-2017
  • 2 // 10-01-2018
I know have the following code, which gives the date of the first encounter of each patient with the hospital.
PID_date = table(M.StudyID, M.processed_at, 'VariableNames', {'PID', 'Datum'});
[Patients, rowN, ~] = unique(PID_date.PID, 'stable');
Datum = PID_date.Datum(rowN);
output_participants = table(Datum, Patients);
Output:
* Datum Patient
* '21-12-2017' 23
* '10-1-2018' 25
* '11-1-2018' 26
* '15-1-2018' 1
* '17-1-2018' 10
* '17-1-2018' 21
* '17-1-2018' 7
* '17-1-2018' 11
* '17-1-2018' 22
* '17-1-2018' 20
* '17-1-2018' 6
* '17-1-2018' 16
* '17-1-2018' 12
* '24-1-2018' 19
* '25-1-2018' 18
* '25-1-2018' 2
I only need to sum these to get the result
"patient 1 visits the hospital on 21-12-2017 and aganin on 28-12-2017. Patient 2 visits the hospital on 12-01-2018. [...] This will result in 2 participants on 10-01-2018"
Hmmmm...
December 21: patient 1 visits once, making one total visit for patient 1 historically; patient 2 has never visited
December 28: patient 1 visits once, making two total visits for patient 1 historically; patient 2 has never visited
-> January 10: No visits today. Like every day since December 28, patient 1 has visited twice historically and patient 2 has never visited
January 12: patient 2 visits once, making one total visit for patient 2 historically; patient 1 has visited twice historically
So by the time of the sampling on January 10, there have been a total of two visits, both by the same patient, and you are expecting 2 // 10-01-2018 output there. But why report on that date? What makes it different than 09-01-2018 ? If the rule is to report counts every Thursday then why no entry for 04-01-2018 and why would you report on Wednesday 10-01-2018 instead of Thursday 11-01-2018 ? And we see from the historical breakdown that by 10-01-2018 that we have had two total visits but only 1 unique visitor, so why would the numeric output be 2 ???
Thanks for your help and effort! But by screwing around, I found the solution.
So the problem was that I wanted to measure the total patient included at a hospital. So every time a new patient ID visits the hopsital, the count is added by one on a specific date. The code I created is as follows:
PID_date = table(M.StudyID, M.processed_at, 'VariableNames', {'PID', 'Datum'});
[Patients, rowN, ~] = unique(PID_date.PID, 'stable');
Adt_P = datetime(PID_date.Datum(rowN), 'Format', 'dd/MM/yyyy');
[HP,EP] = discretize(Adt_P, 'day');
Participants = accumarray(HP, 1);
Result_Participants = table(EP(1:numel(Participants))',cumsum(Participants), 'VariableNames', {'Datum', 'Deelnemers'});

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!