Matlab:过滤时间表以平衡面板数据



我有一个时间表,我想保留那些与我的变量"Id"的每个值相吻合的日子。在这里的例子中,我从我的时间表T开始,其中有三个不同的"id"。我的目标是最终得到一个时间表T_1,其中只有日期与每个"id"之间重合的观察结果。。

T= timetable(datetime({'13/04/2018';'25/04/2018';'28/04/2018';'13/04/2018';'25/04/2018';'13/04/2018'}), [1;1;1;2;2;3],[30;29;45;21;24;8] );
T.Properties.VariableNames = {'Id' 'Price'}
%I want to get:
T_1=timetable(datetime({'13/04/2018';'13/04/2018';'13/04/2018'}), [1;2;3],[30;21;8] );
如果你能帮助我,我将非常感激!

可以使用

T_1 = T('13/04/2018',:);

我建议你阅读关于timerange和时间表的文档。

如果您想将时间表分成多个较小的时间表,您可以按日期选择它们,并将它们存储在单元格数组中:

T_unique = unique(T.Time); % get all unique dates
T_all = cell(size(T_unique)); % init an empty cell array
for k = 1:numel(T_unique)
T_all{k} = T(T_unique(k),:); % and put the timetable for one date in the cell array
end

最新更新