Average of variables in timetable belonging to the same day over several years
9 次查看(过去 30 天)
显示 更早的评论
Hello, I have a timetable, TT(681-by-19), with unevenly spaced daily data from 2010 to 2020 and 19 variables (there might be days that do not occur every year in addition to gaps where entire months are missing). I want to calculate the mean of the variables that correspond to the same day throughout the different years. For example I want to calculate the mean of each of the 19 variables that correspond to 25-oct 2010, 25-oct 2011, ..., 25-oct 2020 and so on for the different dates. That way I would have each unique day-month occurring only once in the output.
Note: I also have the dates seperately as a datetime array and as a numeric array in the form of yyyymmdd (681-by-1) and the 19 variables in a numeric array (681-by-19), if there is a simpler method without the use of the timetable.
Thanks for any assistance.
0 个评论
采纳的回答
Peter Perkins
2022-8-25
Ziad, it seems that "dayofyear" is using "serial day number of year", and as you found, is lumping 29-Feb's with the non-leap 1-Mar's, etc. What you want is to group by month/day. If you use the month and day of month as grouping vars, you get what you want:
dt = [datetime(2012,2,27:31) ...
datetime(2013,2,27:30) ...
datetime(2014,2,27:30) ...
datetime(2015,2,27:30) ...
datetime(2016,2,27:31)]';
x = dt.Day;
tt = timetable(x,RowTimes=dt)
tt.Month = string(month(tt.Time,"name"));
tt.Day = day(tt.Time,"dayofmonth");
means = groupsummary(tt,["Month" "Day"],"mean","x")
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!