How to create daily average of different consecutive years?

4 views (last 30 days)
Hi, I have a question. I am having a dataset made of 6 consecutive years (Hourly Data). I was able to calculate weekly/daily/annual averages of each year but when it's plotted, it is hard to read. I want to create daily averages that is the average of the day in all 6 years. For instance:
Year Value
Day 1 (2000) 1
Day 1 (2001) 0
Day 1 (2002) 1
Day 1 (2003) 1
Day 1 (2004) 0
Day 1 (2005) 1
The average of day 1 for all 6 years is 0.66
[Data is exactly ordered the same in my dataset file.]
I have turned my imported excel file into time table.

Accepted Answer

Adam Danz
Adam Danz on 24 Jul 2019
Edited: Adam Danz on 24 Jul 2019
Assuming your timestamps are in datetime format, use day() with 'dayofyear' flag to convert the timestamps to day-of-year.
Then use findgroups() to group the data by day-of-year. Then use splitapply() to calculate the means.
% Fake timestamp and data, both must be column vectors!
d = datetime('01/01/1950','InputFormat','dd/MM/yyyy') + days(0:3000)';
data = rand(size(d));
doy = day(d,'dayofyear');
[dayGroups, dayKey] = findgroups(doy);
dayMeans = splitapply(@(x)mean(x,'omitnan'),data,dayGroups);
T = table(dayKey,dayMeans,'VariableNames',{'DayOfYear','DayMeans'});
  3 Comments
Guillaume
Guillaume on 26 Jul 2019
I agree with everything Adam said,
If you are on R2018a or later, the above can even be achieved in just one line with groupsummary:
result = groupsummary(yourtableortimetable, 'RowTimes', 'dayofyear', 'mean')

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!