Averaging columns in table using only rows where a condition is met.
4 次查看(过去 30 天)
显示 更早的评论
I would like to find the mean (average) for values in columns 3, 4, 5, 6 using only rows where OOBS.night == 1. This average will be different than the entire column average, as I only want to include a subset of the column values.
I am open to suggestions - perhaps I am going about this wrong!
Thank you.
OOBS = table([hobo_times.OOBS23],[hobo_times.water_elevation_m_NAVD88],[tu_values_23'],[tu_values_27'],[tu_values_29'],[tu_values_35']);
OOBS.Properties.VariableNames = {'times','WtrLvlm','OOBS23','OOBS27','OOBS29','OOBS35'};
OOBS.night = (hour(OOBS.times)>=20 | hour(OOBS.times)<=6);
find(OOBS.night ==1 & OOBS.WtrLvlm>=0.33);
1 个评论
Campion Loong
2022-5-18
This is really just 1-line of code using groupsummary. The first few lines are just making up fake data, since there is no example data attached:
% Make up some data
Times = (datetime(2021,10,1):minutes(10):datetime(2021,10,31))';
WtrLvlm = rand(length(Times),1);
OOBS23 = rand(length(Times),1);
OOBS27 = rand(length(Times),1);
OOBS29 = rand(length(Times),1);
OOBS35 = rand(length(Times),1);
night = timeofday(Times) > hours(18); % assume 'night' means later than 6PM
tt = timetable(Times, WtrLvlm, OOBS23, OOBS27, OOBS29, OOBS35, night)
% This 1-liner is what you are actually after
NightAvg = groupsummary(tt,'night','mean')
% Now it's a slightly different 1-liner if you want to group
% by both 'night' and a 'WtrLvlm' threshold
% (like in your code example, but unlike your descriptions)
groupsummary(tt,["night" "WtrLvlm"],{'none', [0 0.33 Inf]}, "mean")
采纳的回答
David Hill
2022-4-21
n_idx=hour(hobo_times.OOBS23)>=20 | hour(hobo_times.OOBS23)<=6;
m=mean([tu_values_23'(n_idx);tu_values_27'(n_idx);tu_values_29'(n_idx);tu_values_35'(n_idx)]);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!