I have a dataset with ID, time, value. I want to calculate the total time for the my value stays below 1 in the dataset for each subject.
library(data.table)
ID<-rep(1:10,each=10)
time<-rep(1:10,times=10)
value<-rep(c(0.001,0.01,0.05,0.07,0.09,0.096,0.1,0.5,1,2),10)
df<-cbind(ID,time,value)
df<-as.data.frame(df)
# #
df_sum<-setDT(df)[value < 1, diff(range(time)), by = .(ID)]
In this dataset set the expected answer is 8 hours. I am getting 7 hours. Is this the correct way?