Sum of rows based on time range

3 views (last 30 days)
YEMY
YEMY on 24 Jun 2022
Commented: Star Strider on 24 Jun 2022
Hello,
What I am trying to do is sum rows for every 10 days consecutive days. The problem that I have is in my dataframe, is that I have so many missed days, and by applying a function that will sum every 10 rows, it will not be practical as it can sum non-consecutive days. So I am looking for a solution to sum only every 10 consecutive days.
Regards,

Answers (1)

Star Strider
Star Strider on 24 Jun 2022
Try something like this —
T1 = table(datetime('now') + days(sort(randperm(150, 20)))', randi(99,20,1), 'VariableNames',{'Time','Random'});
TT1 = table2timetable(T1)
TT1 = 20×1 timetable
Time Random ____________________ ______ 28-Jun-2022 22:57:08 25 15-Jul-2022 22:57:08 12 21-Jul-2022 22:57:08 29 10-Aug-2022 22:57:08 47 19-Aug-2022 22:57:08 65 05-Sep-2022 22:57:08 75 06-Sep-2022 22:57:08 75 13-Sep-2022 22:57:08 74 15-Sep-2022 22:57:08 21 19-Sep-2022 22:57:08 31 24-Sep-2022 22:57:08 47 26-Sep-2022 22:57:08 71 28-Sep-2022 22:57:08 11 29-Sep-2022 22:57:08 12 08-Oct-2022 22:57:08 52 11-Oct-2022 22:57:08 11
TT1r = retime(TT1, 'regular', 'sum', 'TimeStep',days(10))
TT1r = 15×1 timetable
Time Random ___________ ______ 28-Jun-2022 25 08-Jul-2022 12 18-Jul-2022 29 28-Jul-2022 0 07-Aug-2022 47 17-Aug-2022 65 27-Aug-2022 75 06-Sep-2022 170 16-Sep-2022 78 26-Sep-2022 94 06-Oct-2022 63 16-Oct-2022 0 26-Oct-2022 4 05-Nov-2022 124 15-Nov-2022 20
Here, there are two 10-day intervals in which there are no entries.
.
  2 Comments
YEMY
YEMY on 24 Jun 2022
Thank you for your reply, I thought about the retime function, the problem that I got is that sometimes when a I have a 31 days month, the code consideres the day 31 as the number 1 and starts computing from that day.
Star Strider
Star Strider on 24 Jun 2022
My pleasure.
That is incorrect.
The MATLAB date and time functions are remarkably robust. They do not take into account individual months with respect to the edges of the intervals for summation (in this instance). Here, the code sums over every 10 consecutive days as instructed, regardless of the days of the month. See the retime documentation for details.
.

Sign in to comment.

Tags

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!