Dear R-helper, I have subject 1, 2, 3 and formulation S, MF, MM. I have a problem in calculating AUC. The AUC (0~t) in formulation MF and MM were miscalculated, but I don?t know where my problem is. Here is my code and output. test<-function( InVVTestindex, separateWindows=TRUE) { for( i in 1:length(unique (InVVTestindex$subject))){ x<-InVVTestindex$time[InVVTestindex$subject==i] y<-InVVTestindex$concentration[InVVTestindex$subject==i] v<-InVVTestindex$formulation[InVVTestindex$subject==i] w<-InVVTestindex$pH[InVVTestindex$subject==i] #calculate AUC add<-function(time,concentration){ auc<-0 for(i in 2:length(time)) { auc[i]<-1/2*(time[i]-time[i-1])*(concentration[i]+concentration[i-1]) auc[i]<-auc[i]+auc[i-1] } return(list(auc=auc)) } add<-add(x,y) AUC<-add$auc #Output cat("<< Output >>\n") output<-data.frame(w,i,v,x,y,AUC) colnames(output)<-list("pH","subject","formulation","time","concentration","AUC(0~t)") show(output) cat("\n\n") } } << Output >> pH subject formulation time concentration AUC(0~t) 1 6.8 1 S 0.0 0.00 0.0000 2 6.8 1 S 0.5 110.26 27.5650 3 6.8 1 S 1.0 116.17 84.1725 4 6.8 1 S 1.5 130.20 145.7650 5 6.8 1 S 2.0 145.77 214.7575 6 6.8 1 S 3.0 123.75 349.5175 7 6.8 1 S 4.0 123.05 472.9175 8 6.8 1 S 5.0 82.35 575.6175 9 6.8 1 S 6.0 48.62 641.1025 10 6.8 1 S 8.0 26.51 716.2325 11 6.8 1 S 10.0 3.04 745.7825 12 6.8 1 S 12.0 2.87 751.6925 13 6.8 1 S 24.0 2.74 785.3525 14 6.8 1 MF 0.0 0.00 752.4725 15 6.8 1 MF 0.5 91.67 775.3900 16 6.8 1 MF 1.0 216.78 852.5025 17 6.8 1 MF 1.5 179.91 951.6750 18 6.8 1 MF 2.0 187.02 1043.4075 19 6.8 1 MF 3.0 176.15 1224.9925 20 6.8 1 MF 4.0 171.24 1398.6875 21 6.8 1 MF 5.0 157.83 1563.2225 22 6.8 1 MF 6.0 149.15 1716.7125 23 6.8 1 MF 8.0 142.18 2008.0425 24 6.8 1 MF 10.0 107.56 2257.7825 25 6.8 1 MF 12.0 86.37 2451.7125 26 6.8 1 MF 24.0 9.30 3025.7325 27 6.8 1 MM 0.0 0.00 2914.1325 28 6.8 1 MM 0.5 114.18 2942.6775 29 6.8 1 MM 1.0 160.72 3011.4025 30 6.8 1 MM 1.5 152.28 3089.6525 31 6.8 1 MM 2.0 125.05 3158.9850 32 6.8 1 MM 3.0 118.97 3280.9950 33 6.8 1 MM 4.0 123.26 3402.1100 34 6.8 1 MM 5.0 143.46 3535.4700 35 6.8 1 MM 6.0 137.00 3675.7000 36 6.8 1 MM 8.0 91.48 3904.1800 37 6.8 1 MM 10.0 52.46 4048.1200 38 6.8 1 MM 12.0 15.39 4115.9700 39 6.8 1 MM 24.0 8.00 4256.3100 Best regards, Hsin-Ya Lee