How to get monthly average for ten years monthly data?

Hey Everyone,
I have ten years of montly data, I want to get monthly average.
Thanks in advace,
Riyadh

6 Kommentare

Thank you for answering, I want to get monthly averagre for ten year!
The groupsummary function lets you specify "month" as the groupbins input argument to summarize each month worth of data using whatever you specified as the method input argument ("mean" in the case of a monthly average.)
dpb
dpb am 3 Feb. 2026
Bearbeitet: dpb am 3 Feb. 2026
If as @Stephen23 suggests you use a timnetable, then retime is also very useful...
ttTen=retime(ttData,'monthly','mean');
Use whatever is your timetable variable in place of ttData, of course...
If you have need of additional statistics/calculations over same time period, don't overlook grpstats in Statistics TB
I used it, gives me monthly average for daily data,, howere does'nt work to give the average of ten years in 12 months.
I have 120 month I need the average is 12 month!
Can you show us a small sample of your data and the way you called groupsummary or retime? It's possible a small modification to your call could achieve your ultimate goal.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Cris LaPierre
Cris LaPierre am 3 Feb. 2026
Bearbeitet: Cris LaPierre am 3 Feb. 2026
Here's an example using airlinesmall.csv, which contains US domestic airline flight data from Oct 21, 1987 – Dec 31, 2008.
% Load data
setupExample("matlab/AddKeysValuesExample", pwd)
data = readtable('airlinesmall.csv');
% Create datetime variable
data.Date = datetime(data.Year, data.Month, data.DayofMonth);
data = movevars(data,'Date','Before',1)
data = 123523×30 table
Date Year Month DayofMonth DayOfWeek DepTime CRSDepTime ArrTime CRSArrTime UniqueCarrier FlightNum TailNum ActualElapsedTime CRSElapsedTime AirTime ArrDelay DepDelay Origin Dest Distance TaxiIn TaxiOut Cancelled CancellationCode Diverted CarrierDelay WeatherDelay NASDelay SecurityDelay LateAircraftDelay ___________ ____ _____ __________ _________ _______ __________ _______ __________ _____________ _________ _______ _________________ ______________ _______ ________ ________ _______ _______ ________ ______ _______ _________ ________________ ________ ____________ ____________ ________ _____________ _________________ 21-Oct-1987 1987 10 21 3 642 630 735 727 {'PS'} 1503 {'NA'} 53 57 {'NA'} 8 12 {'LAX'} {'SJC'} 308 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 26-Oct-1987 1987 10 26 1 1021 1020 1124 1116 {'PS'} 1550 {'NA'} 63 56 {'NA'} 8 1 {'SJC'} {'BUR'} 296 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 23-Oct-1987 1987 10 23 5 2055 2035 2218 2157 {'PS'} 1589 {'NA'} 83 82 {'NA'} 21 20 {'SAN'} {'SMF'} 480 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 23-Oct-1987 1987 10 23 5 1332 1320 1431 1418 {'PS'} 1655 {'NA'} 59 58 {'NA'} 13 12 {'BUR'} {'SJC'} 296 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 22-Oct-1987 1987 10 22 4 629 630 746 742 {'PS'} 1702 {'NA'} 77 72 {'NA'} 4 -1 {'SMF'} {'LAX'} 373 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 28-Oct-1987 1987 10 28 3 1446 1343 1547 1448 {'PS'} 1729 {'NA'} 61 65 {'NA'} 59 63 {'LAX'} {'SJC'} 308 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 08-Oct-1987 1987 10 8 4 928 930 1052 1049 {'PS'} 1763 {'NA'} 84 79 {'NA'} 3 -2 {'SAN'} {'SFO'} 447 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 10-Oct-1987 1987 10 10 6 859 900 1134 1123 {'PS'} 1800 {'NA'} 155 143 {'NA'} 11 -1 {'SEA'} {'LAX'} 954 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 20-Oct-1987 1987 10 20 2 1833 1830 1929 1926 {'PS'} 1831 {'NA'} 56 56 {'NA'} 3 3 {'LAX'} {'SJC'} 308 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 15-Oct-1987 1987 10 15 4 1041 1040 1157 1155 {'PS'} 1864 {'NA'} 76 75 {'NA'} 2 1 {'SFO'} {'LAS'} 414 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 15-Oct-1987 1987 10 15 4 1608 1553 1656 1640 {'PS'} 1907 {'NA'} 48 47 {'NA'} 16 15 {'LAX'} {'FAT'} 209 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 21-Oct-1987 1987 10 21 3 949 940 1055 1052 {'PS'} 1939 {'NA'} 66 72 {'NA'} 3 9 {'LGB'} {'SFO'} 354 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 22-Oct-1987 1987 10 22 4 1902 1847 2030 1951 {'PS'} 1973 {'NA'} 88 64 {'NA'} 39 15 {'LAX'} {'OAK'} 337 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 16-Oct-1987 1987 10 16 5 1910 1838 2052 1955 {'TW'} 19 {'NA'} 162 137 {'NA'} 57 32 {'STL'} {'DEN'} 770 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 02-Oct-1987 1987 10 2 5 1130 1133 1237 1237 {'TW'} 59 {'NA'} 187 184 {'NA'} 0 -3 {'STL'} {'PHX'} 1262 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'} 30-Oct-1987 1987 10 30 5 1400 1400 1920 1934 {'TW'} 102 {'NA'} 200 214 {'NA'} -14 0 {'SNA'} {'STL'} 1570 {'NA'} {'NA'} 0 {'NA'} 0 {'NA'} {'NA'} {'NA'} {'NA'} {'NA'}
I have data from 255 months. To get a monthly average, use the following.
% Average each month of the data
monthAvg = groupsummary(data,'Date','month','mean',["ArrDelay","DepDelay"])
monthAvg = 255×4 table
month_Date GroupCount mean_ArrDelay mean_DepDelay __________ __________ _____________ _____________ Oct-1987 448 5.8108 4.5676 Nov-1987 423 7.5311 6.1005 Dec-1987 440 11.97 10.528 Jan-1988 436 11.053 8.1607 Feb-1988 413 8.2138 7.7824 Mar-1988 445 10.014 9.6159 Apr-1988 427 5.8892 6.0776 May-1988 436 5.963 7.3002 Jun-1988 432 5.1729 6.1212 Jul-1988 441 6.5492 7.7986 Aug-1988 447 5.6329 6.5978 Sep-1988 424 2.87 3.0024 Oct-1988 441 4.0619 4.46 Nov-1988 421 8.0529 6.7242 Dec-1988 439 8.6528 7.977 Jan-1989 440 7.2168 8.0069
However, you say you have 120 months of data, and want the result to be 12 months. That would be obtained by averaging by 'monthofyear' insteasd of 'month' (averaging the same month across years).
% Average each month of the data
monthAvg = groupsummary(data,'Date','monthofyear','mean',["ArrDelay","DepDelay"])
monthAvg = 12×4 table
monthofyear_Date GroupCount mean_ArrDelay mean_DepDelay ________________ __________ _____________ _____________ 1 10261 8.5954 9.4439 2 9431 7.3275 8.3786 3 10447 7.5536 9.0161 4 10086 6.0081 7.3522 5 10329 5.2949 6.5943 6 10229 10.264 10.311 7 10568 8.7797 9.5221 8 10649 7.4522 8.3369 9 9974 3.6308 5.2236 10 10757 4.6059 5.8799 11 10220 5.2835 6.6783 12 10572 10.571 11.424

1 Kommentar

I was going to do something similar, @Cris LaPierre but I couldn't remember a longer time sample dataset and got sidetracked seeing if could download something...
The extensive list of choices in groupsummary is helpful, indeed. I've wondered why retime didn't get the same augmented list.

Melden Sie sich an, um zu kommentieren.

Umar
Umar am 3 Feb. 2026
Bearbeitet: Walter Roberson am 3 Feb. 2026
Hi @Riyadh,
I reviewed your question about calculating monthly averages from 10 years of data, and I understand the issue you're facing. You have 120 months of data and need to get 12 average values (one for each calendar month averaged across all 10 years), but the suggestions given so far are still returning 120 values instead of 12.
The key is using the 'monthofyear' binning option in groupsummary, which groups all Januaries together, all Februaries together, and so on, regardless of which year they're from. Here's the solution:
If your data is in a timetable (let's say it's called ttData with a time column and a data column called Temperature):
monthlyAvg = groupsummary(ttData, 'TimeColumnName', 'monthofyear', 'mean', 'Temperature');
This will give you exactly 12 rows - one for each month with the average across all 10 years.
If your data is in a regular table instead of a timetable, you can do this:
yourTable.Month = month(yourTable.DateColumn);
monthlyAvg = groupsummary(yourTable, 'Month', 'mean', 'YourDataColumn');
I've attached a MATLAB script (monthly_average_across_years.m) that demonstrates three different methods to solve your problem. The script uses synthetic data to show exactly how to go from 120 months to 12 monthly averages. When you run it, you'll see it successfully converts 120 data points into 12 averages (one per calendar month), and it includes visualizations showing both the original time series and the final monthly averages.
The script shows that all three methods produce identical results:
* Method 1: Using timetable with groupsummary and 'monthofyear'
* Method 2: Extracting month numbers and grouping
* Method 3: Using findgroups and splitapply for more control
To adapt the code for your actual data, you'll need to:
1. Replace 'monthlyDates' with your actual time variable name
2. Replace 'Temperature' with your actual data variable name
3. Make sure your date column is in datetime format (if it's not, convert it using datetime() function)
4. If you're using MATLAB Mobile, you can comment out the visualization section (lines starting with "figure")
The script will run as-is to show you how the solution works, then you can modify it for your specific dataset. The key takeaway is that 'monthofyear' is specifically designed to bin data by calendar month across multiple years, which is exactly what you need.

4 Kommentare

Sorry about formatting issues.
Thank you so much!
Assuming this will make it easier for everyone:
%% Solution for Riyadh: Calculate Monthly Averages Across 10 Years
% This script demonstrates how to get 12 monthly averages from 10 years of data
% (reducing 120 months to 12 average values - one per calendar month)
clear; clc;
%% Method 1: Using Timetable and groupsummary with 'monthofyear'
fprintf('=== Method 1: Timetable with groupsummary ===\n\n');
=== Method 1: Timetable with groupsummary ===
% Create synthetic monthly data for 10 years (120 months)
startDate = datetime(2014, 1, 1);
endDate = datetime(2023, 12, 1);
monthlyDates = (startDate:calmonths(1):endDate)';
% Generate synthetic data (e.g., temperature, sales, etc.)
% Using random data with seasonal pattern for realism
numMonths = length(monthlyDates);
seasonalPattern = 20 * sin(2*pi*(1:numMonths)'/12) + 50; % Seasonal variation
randomNoise = 5 * randn(numMonths, 1); % Random noise
dataValues = seasonalPattern + randomNoise;
% Create timetable
ttData = timetable(monthlyDates, dataValues, 'VariableNames', {'Temperature'});
% Display first few rows
fprintf('Original data (first 6 months):\n');
Original data (first 6 months):
disp(ttData(1:6, :));
monthlyDates Temperature ____________ ___________ 01-Jan-2014 55.492 01-Feb-2014 60.753 01-Mar-2014 75.192 01-Apr-2014 64.014 01-May-2014 58.491 01-Jun-2014 46.107
fprintf('Total months in dataset: %d\n\n', height(ttData));
Total months in dataset: 120
% Calculate monthly average across all years using 'monthofyear'
monthlyAvg = groupsummary(ttData, 'monthlyDates', 'monthofyear', 'mean', 'Temperature');
% Display results
fprintf('Monthly averages across 10 years:\n');
Monthly averages across 10 years:
disp(monthlyAvg);
monthofyear_monthlyDates GroupCount mean_Temperature ________________________ __________ ________________ 1 10 58.182 2 10 66.007 3 10 71.997 4 10 67.07 5 10 57.516 6 10 53.049 7 10 40.934 8 10 30.711 9 10 31.027 10 10 32.264 11 10 39.407 12 10 49.403
%% Method 2: Using Regular Table (alternative approach)
fprintf('\n=== Method 2: Regular Table with month extraction ===\n\n');
=== Method 2: Regular Table with month extraction ===
% Convert to regular table
regularTable = timetable2table(ttData);
% Extract month number (1-12) from dates
regularTable.MonthNum = month(regularTable.monthlyDates);
% Group by month number and calculate mean
monthlyAvg2 = groupsummary(regularTable, 'MonthNum', 'mean', 'Temperature');
% Add month names for clarity
monthNames = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', ...
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};
monthlyAvg2.MonthName = monthNames';
% Reorder columns for better readability
monthlyAvg2 = monthlyAvg2(:, {'MonthNum', 'MonthName', 'GroupCount','mean_Temperature'});
fprintf('Monthly averages with month names:\n');
Monthly averages with month names:
disp(monthlyAvg2);
MonthNum MonthName GroupCount mean_Temperature ________ _________ __________ ________________ 1 {'Jan'} 10 58.182 2 {'Feb'} 10 66.007 3 {'Mar'} 10 71.997 4 {'Apr'} 10 67.07 5 {'May'} 10 57.516 6 {'Jun'} 10 53.049 7 {'Jul'} 10 40.934 8 {'Aug'} 10 30.711 9 {'Sep'} 10 31.027 10 {'Oct'} 10 32.264 11 {'Nov'} 10 39.407 12 {'Dec'} 10 49.403
%% Method 3: Manual approach using findgroups and splitapply
fprintf('\n=== Method 3: Manual approach (more control) ===\n\n');
=== Method 3: Manual approach (more control) ===
% Extract dates and values as arrays
dates = ttData.monthlyDates;
values = ttData.Temperature;
% Get month numbers
monthNums = month(dates);
% Find groups and apply mean
[G, monthID] = findgroups(monthNums);
monthlyMeans = splitapply(@mean, values, G);
% Create result table
monthlyAvg3 = table(monthID, monthlyMeans, 'VariableNames', {'Month', 'AvgTemperature'});
monthlyAvg3.MonthName = monthNames';
fprintf('Monthly averages using findgroups/splitapply:\n');
Monthly averages using findgroups/splitapply:
disp(monthlyAvg3);
Month AvgTemperature MonthName _____ ______________ _________ 1 58.182 {'Jan'} 2 66.007 {'Feb'} 3 71.997 {'Mar'} 4 67.07 {'Apr'} 5 57.516 {'May'} 6 53.049 {'Jun'} 7 40.934 {'Jul'} 8 30.711 {'Aug'} 9 31.027 {'Sep'} 10 32.264 {'Oct'} 11 39.407 {'Nov'} 12 49.403 {'Dec'}
%% Visualization
fprintf('\n=== Creating Visualization ===\n');
=== Creating Visualization ===
figure('Position', [100, 100, 1000, 600]);
% Subplot 1: Original time series
subplot(2, 1, 1);
plot(ttData.monthlyDates, ttData.Temperature, '-o', 'LineWidth', 1.5, 'MarkerSize', 4);
xlabel('Date');
ylabel('Temperature');
title('Original Monthly Data (10 Years)');
grid on;
% Subplot 2: Monthly averages across years
subplot(2, 1, 2);
bar(monthlyAvg2.MonthNum, monthlyAvg2.mean_Temperature, 'FaceColor', [0.2 0.6 0.8]);
xlabel('Month');
ylabel('Average Temperature');
title('Average Temperature by Month (Averaged Across 10 Years)');
set(gca, 'XTick', 1:12, 'XTickLabel', monthNames);
grid on;
%% Summary
fprintf('\n=== SUMMARY ===\n');
=== SUMMARY ===
fprintf('Starting with %d months of data (10 years)\n', numMonths);
Starting with 120 months of data (10 years)
fprintf('Calculated averages for %d calendar months\n', height(monthlyAvg));
Calculated averages for 12 calendar months
fprintf('\nEach month average represents the mean across %d years\n', ...
floor(numMonths / 12));
Each month average represents the mean across 10 years
%% Export results (optional - for MATLAB Mobile users)
% Uncomment if you want to save results
% writetable(monthlyAvg2, 'monthly_averages.csv');
% fprintf('\nResults saved to: monthly_averages.csv\n');
Thank you so much! appreciate it!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Preprocessing finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 3 Feb. 2026

Kommentiert:

am 4 Feb. 2026

Community Treasure Hunt

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

Start Hunting!

Translated by