Separating table data by year
조회 수: 18 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Scott MacKenzie
2022년 4월 22일
편집: Scott MacKenzie
2022년 4월 22일
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
참고 항목
카테고리
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!