Can one use retime function to calculate mean and sum of the same variable?
Show older comments
Suppose I have a timetable
Visitors
January 1 10
January 3 20
February 10 30
February 12 10
Then I want to create a table
Mean of Visitors Sum of Visitors
January 15 30
February 20 40
Can one do this with retime function? if not, how?
9 Comments
Torsten
on 15 May 2022
Shouldn't Mean of Visitors be 30/31 since January has 31 days ?
Image Analyst
on 15 May 2022
What about the days of the month not listed there? Do we assume the venue was closed on missing days or that the number of visitors those days was 0? Have you seen these functions: splitapply, grpstats, and groupsummary?
dpb
on 15 May 2022
Yes, it can be done with retime to compute multiple outputs (there are examples of doing so in the doc) but for this look at groupsummary
alpedhuez
on 15 May 2022
dpb
on 15 May 2022
grpstats is in Statistics TB; groupsummary is (since R2018a) in base product. Have overlapping functionality on builtin statistics available with some differences but the basics are in both. groupsummary has additional functionality with the 'groupbins' binning scheme that mimics retime type functionality that grpstats doesn't have builtin at all.
Another case where the TB folks and the main product folks step on each others' toes and there's way too much duplication/overlap/redunancy/bloat/...a TB lets some new feature out into the wild; it's popular and picked up in the base product but then there's never a way to clean up the debris field left over...
dpb
on 15 May 2022
alpedhuez
on 15 May 2022
Answers (1)
Gagan Agarwal
on 22 Sep 2023
Hi alpedhuez,
Yes, it is possible to calculate the mean and sum of the same variable using the 'retime' function.
You can refer to the following code snippet as an example to perform the calculation on your table:
% The sample data is stored in a table 'T'
T = table(["January 1"; "January 3"; "February 10"; "February 12"], [10; 20; 30; 10]);
T.Properties.VariableNames = {'Date', 'Value'};
T.Date = datetime(T.Date, 'InputFormat', 'MMMM d')
% T is converted to timetable using 'table2timetable' function.
TT = table2timetable(T);
% 'retime' function is then applied on 'TT' to calculate the mean
T1 = retime(TT, 'monthly', 'mean');
% The 'retime' function is then applied on 'TT' to calculate the sum
T2 = retime(TT, 'monthly', 'sum');
% 'T1' and 'T2' are combined into a single table called 'ans' which displays the sum and mean values
ans = join(T1,T2)
For more detailed information about the 'retime' function, you can refer to the following documentation:
I hope this helps!
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!