Search code examples
rintegrationareacurve

Area under curve for multiple groups in R


I have seven different groups measuring fluorescence over 20 hours. I have made the graphs, and I would now like to calculate the area under the curves of the different groups. A subset of my data looks like this:

Condition Hour N Fluorescence
0 J/cm2    0 2       1537.0
0 J/cm2    1 2       2343.0
0 J/cm2    2 2       1907.0
0 J/cm2    3 2       1632.5
0 J/cm2    4 2       2864.0
0 J/cm2    5 2       3518.5
TBHP       0 2      11231.5
TBHP       1 2      11144.0
TBHP       2 2      11068.5
TBHP       3 2      10881.5
TBHP       4 2      10662.0
TBHP       5 2      10530.0

I have read a lot of different threads on the same subject, but it's always with either only one group or with different columns, and I do not know how to translate that to my data.

Right now I am using the trapz function from the zoo package:

AUC=trapz(Hour, Fluorescence) 

But that only gives me the overall area, or I would have to do the groups individually, which I would prefer not to do.

I would be happy to use a different function or whatever, but my preferred outcome would be something like this:

Condition AUC
0 J/cm2    0 
TBHP       1 

Or set up so I could do a barplot of the different areas.

Thank you in advance!


Solution

  • Assuming your dataframe is called dat and the trapz()-function only requires one parameter you could try:

    aggregate(dat$Fluorescence,by=list(dat$Condition),FUN=zoo::trapz)
    

    (I haven't 100% tried out this call, but the aggregate() function might at least point you into the right direction (I hope...))