How do I find a daily minimum value using retime when I have categorial data in the table?
3 views (last 30 days)
Show older comments
Khandker Ishtiaq
on 1 Sep 2021
Answered: Peter Perkins
on 2 Mar 2022
I have a table with three columns as shown in the below snapshot where column 3 (Material) is categorical. I would like to obtain the daily minimum anomaly time-series and corresponding name of the category from the Material column using the retime function. I can apply retime to estimate minimum but I am not sure how to keep the corresponding row for the 'Material' category. The following code worked but I would like to know how to keep the corrresponding categorical information from column 3.
TT=timetable(time,Anomaly,Material);
subset=TT(:,{'Anomaly'});
TT1=retime(subset,'daily','max');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/726919/image.png)
0 Comments
Accepted Answer
Star Strider
on 1 Sep 2021
My impression is that ‘TT1’ should have all the other information as well.
If it does not, post the data (or a representative subset) as an attachment here (use the paperclip icon) so we can work with it.
.
4 Comments
More Answers (1)
Peter Perkins
on 2 Mar 2022
SS's answer seems to have worked, and I'm late to the party, but I always like to provide an alternative to using datenum, and rowfun is lonely. So here's another way:
>> load sample.mat
>> sampledata = sortrows(table2timetable(sampledata));
Ordinarily, at this point you might turn to retime, but that works on each var separately. So the thing to use is rowfun, which can work on multiple vars, and allows grouping. (SS used max, I've used min).
>> sampledata.Day = dateshift(sampledata.DT,"start","day")
sampledata =
54×3 timetable
DT Anomaly Material Day
____________________ ___________________ ________ ___________
07-Dec-2019 17:00:00 0 Plasctic 07-Dec-2019
09-Dec-2019 16:00:00 -0.525000000000006 Plasctic 09-Dec-2019
09-Dec-2019 21:00:00 -4.27500000000001 Plasctic 09-Dec-2019
[snip]
25-Feb-2020 10:00:00 1.95 Plasctic 25-Feb-2020
29-Feb-2020 16:00:00 0.825000000000003 Metal 29-Feb-2020
29-Feb-2020 20:00:00 -2.75 Metal 29-Feb-2020
>> rowfun(@myFun,sampledata,"GroupingVariable","Day","OutputVariableNames",["Anomaly" "Material"])
ans =
27×4 timetable
DT Day GroupCount Anomaly Material
____________________ ___________ __________ ___________________ ________
07-Dec-2019 17:00:00 07-Dec-2019 1 0 Plasctic
09-Dec-2019 21:00:00 09-Dec-2019 2 -4.27500000000001 Plasctic
17-Dec-2019 17:00:00 17-Dec-2019 1 -0.174999999999997 Plasctic
[snip]
21-Feb-2020 11:00:00 21-Feb-2020 3 1.5 Metal
25-Feb-2020 03:00:00 25-Feb-2020 2 -5.575 Plasctic
29-Feb-2020 20:00:00 29-Feb-2020 2 -2.75 Metal
where
function [aMin,mMin] = myFun(a,m)
[aMin,iMin] = min(a);
mMin = m(iMin);
end
0 Comments
See Also
Categories
Find more on Timetables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!