Separating table data by year

14 views (last 30 days)
Nathan Brown
Nathan Brown on 22 Apr 2022
Edited: Scott MacKenzie on 22 Apr 2022
From a given table of data from 1900 to 2017 of low temperatures i want to separate by year then run functions to find the number of days per year where temperatures were lower than the average over the period of 1900-2017.
below is what was provided as the example, but im having trouble separating out specified years to work on
gDays = findgroups(day(BostonTemps.Date, 'dayofyear'));
avgTmin = splitapply(@mean, BostonTemps.Tmin, gDays);
stdTmin = splitapply(@std, BostonTemps.Tmin, gDays);
[gYears,years] = findgroups(year(BostonTemps.Date));

Answers (1)

Scott MacKenzie
Scott MacKenzie on 22 Apr 2022
Edited: Scott MacKenzie on 22 Apr 2022
Here's what I put together using random temperatures between 30 and 100 over the period of interest. Since the temperatures are random, about half of the 365 or 366 days each year are lower than the period average.
% test data (random hourly temperatures from 1900 to 2017)
dt1 = datetime('1900-01-01', 'InputFormat','yyyy-MM-dd');
dt2 = datetime('2017-12-31', 'InputFormat','yyyy-MM-dd');
dt = (dt1:hours(1):dt2)';
tmp = randi([30 100], length(dt),1); % random temperatures between 30 and 100
% organize in timetable
TT = timetable(dt, tmp);
% retime to get daily mean temperature
TT1 = retime(TT, 'daily', 'mean');
% average daily temperature for the period 1900 to 2017
epochAverage = mean(TT1.tmp);
% add a column flagging each day where the average temperature < epochAverage
TT1.LowerThanAverage = TT1.tmp < epochAverage;
% retime to get the number of days each year with temperature < epochAverage
TT2 = retime(TT1, 'yearly', 'sum');
epochAverage % average temperature over the period of interest
epochAverage = 64.9968
TT2([1:5 (end-4):end], 2) % results for first 5 years and last 5 years
ans = 10×1 timetable
dt LowerThanAverage ___________ ________________ 01-Jan-1900 189 01-Jan-1901 191 01-Jan-1902 180 01-Jan-1903 189 01-Jan-1904 172 01-Jan-2013 164 01-Jan-2014 174 01-Jan-2015 174 01-Jan-2016 181 01-Jan-2017 184


Find more on Tables 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!