Summarizing timetable data when categorical variables are present

I have a table with repeating timestamped ('long-form') data and categorical variables that are assigned to each measurement (in this case temperature and humidity). What I want to do is summarize daily means while nesting within the categorical variables. Is this possible to do using the retime function?
'06/01/2015 00:00' 9.16300000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'
'06/01/2015 00:15' 9.21300000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'
'06/01/2015 00:30' 9.21300000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'
'06/01/2015 00:45' 9.16300000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'
'06/01/2015 01:00' 9.06400000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'
'06/01/2015 01:15' 8.81700000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'
'06/01/2015 01:30' 8.71700000000000 100 'Control' 'C1' 'S1' 'Open' 'Treatment' 'Year 0'

Answers (2)

Cameron, the answer is not really. retime is all about time.
I thik what you want is something like varfun using time and some other variable(s) as GroupingVariables, or as Walter suggests, findgroups and splitapply.
Also, it looks like you have categorical data, but stored as text variables. You might be happier if you explicitly converted those text vars to be categoricals.

4 Comments

findgroups, splitapply, but the function you splitapply can possibly be retime. That is you look like you want to retime per category.
This is exactly what I would like to do to no avail. retime per category using splitapply. help appreciated!
I have a wrapper function to call retime for monthly data
function x = retimeMonthly(TT)
x= retime(TT, 'monthly', 'lastvalue');
end
Then I try this:
dates = datetime(['2020-08-10'; '2020-08-11'; '2020-08-12'; '2020-08-13']); % dates for the rows below
t = array2table({'A', '10'; % A/B is the category
'B', '11';
'A', '12';
'B', '13';});
t.date = dates; % add date column to table
TT = table2timetable(t) % convert to TT
[groups, groupNames] = findgroups(TT.Var1) % find groups based on category A/B
retimeMonthly(TT) % this is just an example to show the retimeMonthly funciton works. Without groups it returns the last record, as expected
splitapply(@retimeMonthly, TT, groups)); % I want this to return the last montly record by category
I get this error
Error using splitapply (line 132)
Applying the function 'retimeMonthly' to the 1st group of data generated the following error:
Too many input arguments.
I want to use this in my production code with a table that has many more columns. I want to return the table with the last record for every month. If there is better way to do this, I'm happy to adjust. I thought splitapply would be elegant.
Thank you kindly!
When you use splitapply() with a table() object, the function is passed one parameter for each table variable.
Perhaps
splitapply(@(varargin)retimeMonthly(cell2table(varargin, 'VariableNames', TT.Properties.VariableNames), TT, groups)
Thanks, I had success with this
splitapply(@(varargin)retimeMonthly(table(varargin{1,:}, 'VariableNames', t.Properties.VariableNames)), t, groups)
(but I also had to "re-timeable" the function argument inside the retimeMontly.
THANK YOU!

Sign in to comment.

Categories

Products

Release

R2018b

Asked:

on 30 Nov 2018

Community Treasure Hunt

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

Start Hunting!