Aim: I want to calculate the percentage of observations (CONC) that lie outside 90% CI for all subjects in the data.
My data frame contains the following columns:
df <-
ID TIME CONC CI90low CI90hi
1 4 9.38 0.870240934 133.6468179
1 5 37.5 0.936887451 140.4165014
1 6 50.9 1.804344597 16.7551025
1 8 53.5 55.34913078 146.1486235
1 10 64.8 8.433188849 126.9535201
1 12 47.8 15.48328251 94.23716498
1 24 19.4 2.457364534 34.00074335
1 36 5.54 1.107788098 22.38902995
1 48 2.52 0.456572767 14.28822964
2 1 7.23 0.309733729 52.68946657
2 1.5 27.1 0.705395145 100.630645
2 10 51.1 9.78008354 134.8669611
2 12 37.1 5.500102861 94.25775578
I thought of one possible way to accomplish this but I am not sure how to code it in R.
My idea is to add a new column in the data frame then:
1) For each subject at each time point (ID,TIME
) check if the concentration (CONC
) lies between the lower and upper limit of the 90% CI provided. If YES, then add a value of Zero0
to the new column if NO then add a value of 1
. I tried ifelse
but wasn't able to nail it down.
2)count the numbers of zeros in the column. Then:
% of observations outside the 90%CI = total number of ONEs/length(df)*100%
I would appreciate your help in coding this. Perhaps, you may have another way of doing it.