In my last question, I asked how to find a rolling mean, thank you for your help!
However, now that I have the lowest metabolic value of the rolling mean, my table is no longer in order. I want to flag the O2 value from that row, unsort it back into its original position, and average that 02 value with the next 4 values below it. Is this possible?
I'm just not sure how to set the flag to let R know that I want to keep using this value after I've unsorted the table
Alternatively, rather than unsorting and flagging my specified O2 value, would it be easier to just flag the timestamp for that O2 value, find that time in the original sheet, and then select the O2 from that row to do the average with the 5 values below it?
The value I am looking to get is the average V02 value from 13:36 to 14:01 which is: 0.738622117
Date Time kCal VO2 VCO2 Lowest Average
2020/08/11 13:36:00 0.1796796 0.6212131 0.5481290 1.290649
2020/08/11 13:41:00 0.1796833 0.6212261 0.5481405 1.412320
2020/08/11 18:06:00 0.2475342 0.8529993 0.7080062 1.540823
2020/08/11 13:46:00 0.1796903 0.6212505 0.5481620 1.551518
2020/08/11 18:01:00 0.3073857 1.0778390 0.9221587 1.580908
etc.
EDIT: From Second Solution:
> August11RMR6[,..I:=.I]
> setorder(August11RMR6, VO2_M_1, na.last=T)
> August11RMR6[..I%in%(..I[1]+(0:4)),]
Output:
DateTime kcal_hr_M_1 VO2_M_1 VCO2_M_1 Sum_6period ..I
1: 2020/08/11 13:36:00 0.1796796 0.6212131 0.5481290 1.290649 1
2: 2020/08/11 13:41:00 0.1796833 0.6212261 0.5481405 1.412320 2
3: 2020/08/11 13:46:00 0.1796903 0.6212505 0.5481620 1.551518 4
4: 2020/08/11 18:06:00 0.2475342 0.8529993 0.7080062 1.540823 3
5: 2020/08/11 18:01:00 0.3073857 1.0778390 0.9221587 1.580908 5
From First Solution:
> August11RMR6[,..I:=.I]
> row.num <- as.numeric(August11RMR6[order(VO2_M_1)[1], "..I"])
> row.num
[1] 1
> August11RMR6[row.num+(0:4)]