take averages of obvervations for each month

1 回表示 (過去 30 日間)
alpedhuez
alpedhuez 2020 年 7 月 5 日
コメント済み: madhan ravi 2020 年 7 月 5 日
suppose I have a table S
date income
----------------------------
1/1/2020 100
1/2/2020 20
I want to create a table T of mean of income at each month like
month income average
----------------------------
1/2020 50
2/2020 20
So is it that I first create
T.yyyyMM=datetime(S.date, 'InputFormat','MM/dd/yyyy', 'Format','yyyyMM');
then use
T.accumarray(T.yyyyMM,S.income,[],@mean)
Does it make sense?

採用された回答

jonas
jonas 2020 年 7 月 5 日
編集済み: jonas 2020 年 7 月 5 日
Try this instead
retime(T,'monthly','mean')
where T is a timetable
example
t = (datetime(2020,1,1):days(1):datetime(2021,1,1))';
income = 10+rand(numel(t),1).*4;
T = timetable(t,income)
T_Monthly = retime(T,'monthly','mean')
  3 件のコメント
jonas
jonas 2020 年 7 月 5 日
I don't understand why this case is more complicated. Obviously you cannot take an average of a column with strings, but you can easily exclude it. Just pass specific columns to retime.
In your case it would be
T_Monthly = retime(T(:,1),'monthly','mean')

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by