Dayname for year using groupsummary

2 Ansichten (letzte 30 Tage)
Ajay Nair
Ajay Nair am 7 Mai 2020
Beantwortet: TARUN am 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.

Antworten (1)

TARUN
TARUN am 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:

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by