How to group data according to date in years?
조회 수: 10 (최근 30일)
이전 댓글 표시
I have a table with data of specific days of the year over a range of 10 years, e.g.
1.Jan.2013 12
8.Jan.2013 10
...
23.May.2013 15
30.May.2013 12
...
8.Nov.2013 5
...
1.Jan.2014 16
8.Jan.2014 11
...
23.May.2014 11
30.May.2014 10
...
23.May.2015 8
30.May.2015 9
...
(in other words: I have data over a period of 10 years of the 1.Jan,8.Jan,....,23.May,30.May,.....8.Nov,...)
Now I would like to have a groupsummary according to these dates over all the years, e.g.
Date mean func1 median etc.
1.Jan 11 6 4
8.Jan 10 5 3
...
23.May 9 5 10
...
In the end I would like to plot all the function values (mean, median, func1) over Date (1.Jan, 8.Jan, ...,23.May,... on x-axis).
How can I achieve this?
I tried with 'dayofyear', but this is a bit annoying because of leap years...
댓글 수: 0
채택된 답변
Star Strider
2022년 1월 25일
Since 'dayofyear' won’t work, break the dates down into two new variables, specifically the day and the month, then group on them. To get them back to the 'dd.MMM' format requires a bit of creativity, however it can be done —
T1 = table({'1.Jan.2013'; '8.Jan.2013'; '1.Jan.2014'; '8.Jan.2014'}, [12; 10;, 16; 11], 'VariableNames',{'Date','Something'})
T1.Var1 = datetime(T1.Date, 'InputFormat','dd.MMM.yyyy','Format','dd.MMM.yyyy')
T1.Day = day(T1.Date);
T1.Month = month(T1.Date)
G1 = groupsummary(T1,{'Day','Month'},{'mean','median','std',@(x)std(x)/sqrt(numel(x))},'Something')
G1.DayMonth = datetime(zeros(size(G1.Month)),G1.Month,G1.Day, 'Format','dd.MMM')
G1.Properties.VariableNames{end-1} = 'StdErrMean_Something';
G2 = G1(:,[end 3 4:end-1])
Add other summary statistics as desired.
With respect to plotting them, see Plot Dates and Durations for a complete dsescription of the process.
.
댓글 수: 2
Star Strider
2022년 1월 25일
As always, my pleasure!
This is an interesting problem. Thank you for posting it!
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!