I have a table in which I need to count the number of patient having a certain condition, or if not or if no info is available. Ideally, for each patient, there is the same information about cond in each line of the data, but sometimes the data contains errors, as illustrated by the first line that should have cond=1.
I am looking for a solution that allows me to count 0’s, 1’s and NA’s for each patient_id. In the example below this should give me one observation of 0’s, two observations of 1’s, and 1 observation of NA’s.
Have:
patient_id | cond |
---|---|
1 | 0 |
1 | 1 |
1 | 1 |
2 | 0 |
2 | 0 |
2 | 0 |
3 | |
3 | |
4 | 1 |
4 | 1 |
4 | 1 |
4 | 1 |
Want:
0’s | 1’s | NA’s | 999 |
---|---|---|---|
1 | 2 | 1 | 0 |
0’s is the number of occasions that a patient has only 0’s or 0’s and NA’s.
1’s is the number of occasions that a patient has any 1’s.
NA’s is the number of occasions that a patient has only NA’s.
There should not be any cases in which a patient has any other values, but if so, the result should be ‘999’.
With the function table(df$cond, useNA = “always”) I can get the frequency of 1, 0 and NA, but that counts every row and my data has several rows for each patient.