How to sum up over dates?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have table with T
date location sales
--------------------
1/1 NY 100
1/2 NY 105
1/1 LA 70
1/2 LA 90
I want to sum up over dates to create T1 with
1/1 170
1/2 195
What is the next step?
1 件のコメント
alpedhuez
2021 年 7 月 29 日
https://www.mathworks.com/matlabcentral/answers/406864-how-do-i-sum-over-one-dimension-of-a-multidimensional-array
採用された回答
Cris LaPierre
2021 年 7 月 29 日
編集済み: Cris LaPierre
2021 年 7 月 29 日
Convert your dates to datetimes and use groupsummary with the groupbins input set to 'day' and your method set to 'sum', and datavars specified as your sales variable. Use the following syntax.
13 件のコメント
alpedhuez
2021 年 7 月 29 日
what for groupvars?
Cris LaPierre
2021 年 7 月 29 日
What do you want to group by?
alpedhuez
2021 年 7 月 29 日
So is it
groupsummary(T,'date','day','sum','sales');
?
Cris LaPierre
2021 年 7 月 29 日
編集済み: Cris LaPierre
2021 年 7 月 29 日
Looks good to me, but you have the data, so why not run it and see if you get the result you want?
alpedhuez
2021 年 7 月 29 日
Now suppose I have two variables
date location sales visitors
--------------------
1/1 NY 100 3
1/2 NY 105 5
1/1 LA 70 6
1/2 LA 90 10
I want to sum sales and visitors for each date:
date sales visitors
--------------------
1/1 170 9
1/2 195 15
Does it
groupsummary(T,'date','day','sum',{'sales','visitors'});
?
Cris LaPierre
2021 年 7 月 29 日
What happens when you run that line of code?
alpedhuez
2021 年 7 月 29 日
Yes it works fine. Now is it possible to "sum" for a variable and "mean" for another variable?
Cris LaPierre
2021 年 7 月 29 日
編集済み: Cris LaPierre
2021 年 7 月 29 日
Yes. To take the sum and mean of sales and visitors, I'd do this
groupsummary(T,'date','day',{'sum','mean'},{'sales','visitors'});
alpedhuez
2021 年 7 月 29 日
But then how it goes with the sum for two variable and mean for one vaiable?
Cris LaPierre
2021 年 7 月 29 日
編集済み: Cris LaPierre
2021 年 7 月 29 日
I misspoke earlier. All methods are applied to all datavars. With groupsummary, it is not possible to separate the stats by variable. To do that, i would probably compute the all stats for all variables, then remove the columns I do not want.
alpedhuez
2021 年 7 月 29 日
No I mean, suppose I now have three variables sales, visitors, profits and want to take sum of sales and visitors and mean of profits. Is it
groupsummary(T,'date','day',{'sum','sum','mean'},{'sales','visitors','profits'});
?
alpedhuez
2021 年 7 月 29 日
What will be pseudo code?
Cris LaPierre
2021 年 7 月 29 日
See my previous reply. All methods are applied to all datavars.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Dates and Time についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
