I have a data frame, my data frame is like this: except the last column is not there. I mean I do not have formula column and here my purpose is to calculate that column.
but how it has been calculated?
the formula for the last column is: for each patientNumber, number of Yes/total number of questions has been answered by the patient.
for example for the patient number one
:there is 1 Yes and 2 No
, so it has been 1/3
for patient two, in year 2006, month 10
, we can not see Yes
the three questions are no, so it has been calculated 0
PatientNumber QT Answer Answerdate year month dayofyear count formula
1 1 transferring No 2017-03-03 2017 3 62 2.0 (1/3)
2 1 preparing food No 2017-03-03 2017 3 62 2.0 (1/3)
3 1 medications Yes 2017-03-03 2017 3 62 1.0 (1/3)
4 2 transferring No 2006-10-05 2006 10 275 3.0 0
5 2 preparing food No 2006-10-05 2006 10 275 3.0 0
6 2 medications No 2006-10-05 2006 10 275 3.0 0
7 2 transferring Yes 2007-4-15 2007 4 105 2.0 2/3
8 2 preparing food Yes 2007-4-15 2007 4 105 2.0 2/3
9 2 medications No 2007-4-15 2007 4 105 1.0 2/3
10 2 transferring Yes 2007-12-15 2007 12 345 1.0 1/3
11 2 preparing food No 2007-12-15 2007 12 345 2.0 1/3
12 2 medications No 2007-12-15 2007 12 345 2.0 1/3
13 2 transferring Yes 2008-10-10 2008 10 280 1.0 (1/3)
14 2 preparing food No 2008-10-10 2008 10 280 2.0 (1/3)
15 2 medications No 2008-10-10 2008 10 280 2.0 (1/3)
16 3 medications No 2008-10-10 2008 12 280 …… ………..
Update 1
Also, what if the formula change a little bit:
if the patient visit the hospital once a year
, the same formula as it is multiple by 2. for example, for year 2017
there is just one month related to that patient, so it means the patient reached just one time during the year. in this case the above formula multiple by 2 works.
(why because my window should be every 6 month, so if the patient has not come every 6 month I am assuming the same record is happening)
But if there is several records during one year for one patient, it should be multiplied 2/the number of record on that year
.
for example at year 2007
, the patient reached the hospital 2 times
once in month 4
and another in month 12
so in this case the same formula should be multiplied by 2/2