Sum of rows based on time range
3 次查看(过去 30 天)
显示 更早的评论
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,
0 个评论
回答(1 个)
Star Strider
2022-6-24
Try something like this —
T1 = table(datetime('now') + days(sort(randperm(150, 20)))', randi(99,20,1), 'VariableNames',{'Time','Random'});
TT1 = table2timetable(T1)
TT1r = retime(TT1, 'regular', 'sum', 'TimeStep',days(10))
Here, there are two 10-day intervals in which there are no entries.
.
2 个评论
Star Strider
2022-6-24
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.
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!