How can I organize data into monthly means/averages across all years? And then plot it in a bar graph?
조회 수: 11 (최근 30일)
이전 댓글 표시
Hello, I would like to make a barplot of average monthly number of students, in other words the mean of the total students across all school years. So the x-axis would read August, September, October, etc. and the y axis would be the average number of students in that month across all years.
I am not sure where to start on this, below is just some code for students per year, not related to what I am trying to find.
Table = readtable("Student_data.xlsx"); % Read in data
%Part 3: Assign school year (SY) to each daily observation
SY = zeros(size(Table.month));
for i = 1:height(Table)
if Table.month(i)>=8 % months "greater" than or equal to August (month 8) are in the following schoolyear, so add 1 yr
SY(i) = Table.year(i) + 1;
else
SY(i) = Table.year(i); % months "lesser" than August are in the regular year
end
end
Table.schoolyear = SY(:); % add "schoolyear" as a column in Table
%For each school year, sum up the total students just for that year.
g = findgroups(Table.schoolyear); % "group" the unique school years
students = splitapply(@sum, Table.students, g);% sum the total students of each school year "group"
year = splitapply(@mean, Table.schoolyear, g);
%Make a Barplot for annual total students
bar(year,students);
ylabel("Students");
xlabel("School Year");
title("Annual Student Totals");
% Now make a barplot of monthly mean students
% I am stuck here
댓글 수: 1
Antoni Garcia-Herreros
2023년 4월 26일
Hello Macy,
You could try something like this:
Table = readtable("Student_data.xlsx");
Mat=table2array(Table);
Months=zeros(12,1);
for i=1:12
imonth=Mat(Mat(:,1)==i,4);
Months(i)=sum(imonth);
end
MonthLabel={'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
bar(Months);
ylabel("Students");
xlabel("Month");
title("Monthly Student Totals");
set(gca,'xticklabel',MonthLabel);
채택된 답변
Sugandhi
2023년 4월 26일
Hi,
I understand that you would like to make a barplot of average monthly number of students.
You can solve your problem something like this:
Table = readtable("Student_data.xlsx"); % Read in data
monthNames= {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
SY = zeros(size(Table.month));
for i = 1:height(Table)
if Table.month(i)>=8 % months "greater" than or equal to August (month 8) are in the following schoolyear, so add 1 yr
SY(i) = Table.year(i) + 1;
else
SY(i) = Table.year(i); % months "lesser" than August are in the regular year
end
end
Table.schoolyear = SY(:); % add "schoolyear" as a column in Table
%For each month, sum up the total students for all year and divide by total number of years.
g = findgroups(Table.schoolyear);% "group" the unique school years
year = splitapply(@mean, Table.schoolyear, g); % get unique years
TotalYears= size(year);
TotalYears=TotalYears(1,1); %get total number of years
g1 = findgroups(Table.month); % "group" the unique months
totalStudents = splitapply(@sum, Table.students, g1); % sum the total students of each school month "group"
months=splitapply(@mean, Table.month, g1); % get unique month
avgStudents= totalStudents/TotalYears(1); % get average monthly number of students.
%Make a Barplot for average monthly number of students
bar(months,avgStudents);
ylabel("Students");
xlabel("Month");
title("Average monthly number of students");
set(gca,'xticklabel',monthNames);
댓글 수: 0
추가 답변 (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!