如何对动物园对象中的多个列求和
这应该是非常简单的。我有一个动物园对象,有 500 个时间序列(每个时间序列不同的产品)和 250 个销售周期。动物园对象是完美的矩形,所有系列都包含每个时间点的观察结果。我的索引列是一个非常简单的 1...250,
我的困难在于尝试聚合所有时间序列以形成“总销售额”系列。
我尝试过使用聚合,它似乎专注于聚合行,例如将几天聚合成几个月。但我想保留每个时间段,只需将时间序列聚合在一起。这是我的动物园对象的简化版本,如下所示,只有 5 个系列。
head(z.all)
1 2 3 4 5
1 1232.205 1558.056 993.9784 1527.066 359.6946
2 1262.194 1665.084 1092.0105 1834.313 484.5073
3 1301.034 1528.607 900.4158 1587.548 525.5191
4 1014.082 1352.090 1085.6376 1785.034 490.9164
5 1452.149 1623.015 1197.3709 1944.189 600.5150
6 1463.359 1205.948 1155.0340 1528.887 556.6371
当我尝试使用以下两个命令之一进行聚合时,我得到的数据与原始动物园对象中的数据完全相同!
aggregate(z.all[,1:num.series], index(z.all), sum)
aggregate(z.all, index(z.all), sum)
不过,我可以通过这样做进行聚合,尽管对于 500 列来说这是不现实的!如果可能的话,我想避免使用循环。
z.all[,1] + z.all[,2]
如果这不是正确的协议,我深表歉意,这是我在该网站上的第一篇文章。
This should be exceptionally simple. I have a zoo object with 500 times series (each one a different product) and 250 periods of sales. The zoo object is perfectly rectangular, all series contain an observation at each point in time. My index column is a very simple 1...250,
My difficulty is in trying to aggregate all of the time series to form a "Total Sales" series.
I've tried using aggregate, which seems focused on aggregating rows e.g. days into months. But I want to keep every time period, just aggregate the time series together. This is a simplified version of my zoo object shown below with only 5 series.
head(z.all)
1 2 3 4 5
1 1232.205 1558.056 993.9784 1527.066 359.6946
2 1262.194 1665.084 1092.0105 1834.313 484.5073
3 1301.034 1528.607 900.4158 1587.548 525.5191
4 1014.082 1352.090 1085.6376 1785.034 490.9164
5 1452.149 1623.015 1197.3709 1944.189 600.5150
6 1463.359 1205.948 1155.0340 1528.887 556.6371
When I try to aggregate using either of the following 2 commands I get exactly the same data as in my original zoo object!!
aggregate(z.all[,1:num.series], index(z.all), sum)
aggregate(z.all, index(z.all), sum)
However I am able to aggregate by doing this, though it's not realistic for 500 columns! I want to avoid using a loop if possible.
z.all[,1] + z.all[,2]
Apologies if this is not the right protocol, it's my first post in this site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望我正确理解你想要什么。但如果您要查找的是 rowsum:
直接从
base
包中查找。 (?rowSums
)。此函数将一行中的所有值相加:相反的是
colSums()
,它将对每一列求和。I hope i understood correctly what you want. But if it is a rowsum you are looking for:
directly from the
base
package. (?rowSums
). This function adds all values along one row:the opposite would be
colSums()
which would sum each column.