Using R, I am trying to calculate the total time duration for each individual where this time duration is the time spent above certain threshold.
For example, in the plot below I have the concentration data for 3 subjects (ID), and I would like to find the time (x axis) spent above the blue dashed line for each individual. the data set structure would be something like:
head(dataset)
ID time CP
1 1 0.0 0.00000000
2 1 0.0 0.00000000
3 1 0.5 0.03759806
4 1 1.0 0.12523455
5 1 1.5 0.23483219
6 1 2.0 0.34820905
Solid lines represent the concentrations for 3 different subjects
I tried to use the following code:
library(data.table)
TAbove<-setDT(dataset)[CP > .05, diff(range(time)), by = ID]
However, this code that it calculates the time duration from first rise above dashed blue line to the last drop. For example for the green line ID, see the black line.
How can I write a code that takes into account the times where the concentrations drop below the dashed line, by excluding them. the final result would be a total time duration of all the times above the dashed blue line. like below
I think your solution is almost perfect, just leave out range
. I tried the following on an extended dataset (added a few entries)
> dat <- fread("ID time CP
+ 1 0.0 0.00000000
+ 1 0.0 0.00000000
+ 1 0.5 0.03759806
+ 1 1.0 0.12523455
+ 1 1.5 0.23483219
+ 1 2.0 0.34820905
+ 1 3.0 0.5
+ 2 0.0 0.5
+ 2 0.5 0.01
+ 2 1.0 0.2")
with the following result:
> dat[CP > .05, diff(time), by = ID]
ID V1
1: 1 0.5
2: 1 0.5
3: 1 1.0
4: 2 1.0
Edit: Calculation with original data set
Using the original data set
dataset <- fread("ID time CP
1 0.0 0.00000000
1 0.0 0.00000000
1 0.5 0.03759806
1 1.0 0.12523455
1 1.5 0.23483219
1 2.0 0.34820905")
we get the following result:
> dataset[CP > .05, diff(time), by = ID]
ID V1
1: 1 0.5
2: 1 0.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With