for文を使わない日付欠損値の挿入

13 views (last 30 days)
涼輔 前田
涼輔 前田 on 16 Dec 2021
現在、グループ番号毎に2019年3月1日から2020年3月31日までのdatacountが1以上のものに関してのデータがあります。
そこでdatacountが0の日付のデータを挿入するために以下のようなfor文を使っているのですがデータ容量が非常に大きく、膨大な時間がかかってしまいます。そこでfor文を使わない欠損日付の挿入方法はないでしょうか。retimeを使えば速いとは思いますが、全てのグループそれぞれに対して欠損日付を挿入する方法が分かりません。datacount,日付,グループ番号は変数Tに格納されています。
[g, groupId] = findgroups(T.group);
timeRange = datetime(2019,4,1):days(1):datetime(2020,3,31); % 2019/4/1~2020/3/31の1日毎の要素
% グループ番号毎にtimeが2019/4/1~2020/3/31で、datacountが0のテーブル
newT = table;
newT.time = repmat(timeRange, [1, length(groupId)])';
newT.datacount(:) = 0;
newT.group(:) = sortrows(repmat(groupId', [1, length(timeRange)])');
% 変数Tに含まれている日付とグループ番号の場合は、Tのdatacountを代入
for n=1:height(T)
idx = (T.time(n) == newT.time) & (T.group(n) == newT.group);
newT.datacount(idx) = T.datacount(n);
end

Answers (0)

Categories

Find more on データ型の変換 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!