Dayname for year using groupsummary

2 views (last 30 days)
Ajay Nair
Ajay Nair on 7 May 2020
Answered: TARUN on 8 Aug 2025
i have table for 1 full year.
i need to regroup table into dayname for each month.
if i use
groupsummary(MyTable, 'Date_Time', 'dayname', @nanmean);
am getting 7 days averaged for 1 year but i need it for indvidual months like each month days are averaged.
final output should be 7 rows 12 colums.
7 represnts dayname 12 represents each month.

Answers (1)

TARUN
TARUN on 8 Aug 2025
The reason you are getting just 7 rows is because
groupsummary(MyTable, 'Date_Time', 'dayname', @nanmean);
averages across the entire year.
To get weekday averages for each month (a 7×12 matrix), you’ll need to group by both day name and month.
You can use the following workaround to fix it:
MyTable.Month = month(MyTable.Date_Time);
MyTable.DayName = categorical(day(MyTable.Date_Time, 'longname'));
Summary = groupsummary(MyTable, {'DayName', 'Month'}, 'nanmean');
Result = unstack(Summary, 'nanmean_YourVar', 'Month');
‘YourVar’ is the variable that you are using for averaging.
This gives you 7 rows (Mon–Sun) and 12 columns (Jan–Dec), just as needed.
Please refer to this documentation for more details:

Categories

Find more on Tables in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!