Dayname for year using groupsummary

2 次查看(过去 30 天)
Ajay Nair
Ajay Nair 2020-5-7
回答: TARUN 2025-8-8
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.

回答(1 个)

TARUN
TARUN 2025-8-8
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:

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by