Separating table data by year
14 views (last 30 days)
Show older comments
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));
0 Comments
Answers (1)
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
TT2([1:5 (end-4):end], 2) % results for first 5 years and last 5 years
0 Comments
See Also
Categories
Find more on Dates and Time 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!