如何在具有多列的动物园对象中使用“apply.monthly”

发布于 2024-12-02 17:01:59 字数 806 浏览 1 评论 0原文

我有一个名为 pp 的动物园对象,其中包含每日数据和 77 列,如下所示:

            X02R X03N X04K X04N X04R X06I X06N X08J X08P X09O X11O X12L X14N X15G X16K  (...)
1961-01-01  8.3  5.2  3.2  0.0  8.7  5.2 15.0  7.2 11.5 13.0  0.0  4.9  0.0  2.9  6.0   
1961-01-02  1.1  3.2 10.0  0.0  0.0  3.5  0.0  8.7  0.4  1.2  0.0  0.4  0.0  3.2  0.2    
1961-01-03 12.0  4.2 50.5  0.0  9.0 38.5 15.0 31.7  1.7  8.7  9.0 69.2  4.2 22.2  9.2 
(...)  

我想对每一列使用 apply.monthly ,所以最终我仍然有 77 列,但每月数据而不是每日数据。我试过 apply.monthly(pp, FUN=sum) 但结果是一个只有一列的动物园对象(我认为是添加所有列)。

我还尝试了一个循环:

for (i in 1:77) { mensal<-apply.monthly(pp[,i], FUN=sum) } 但它也只生成一列(第 77 列)。我也许可以通过一些试验和错误来使循环工作,但需要很长时间才能计算(我有 17897 行和 77 列),我想有一种更简单的方法可以在不使用循环的情况下完成此操作...所以如果你知道怎么办,请帮忙。谢谢!

I have a zoo object called pp with daily data and 77 columns that looks like this:

            X02R X03N X04K X04N X04R X06I X06N X08J X08P X09O X11O X12L X14N X15G X16K  (...)
1961-01-01  8.3  5.2  3.2  0.0  8.7  5.2 15.0  7.2 11.5 13.0  0.0  4.9  0.0  2.9  6.0   
1961-01-02  1.1  3.2 10.0  0.0  0.0  3.5  0.0  8.7  0.4  1.2  0.0  0.4  0.0  3.2  0.2    
1961-01-03 12.0  4.2 50.5  0.0  9.0 38.5 15.0 31.7  1.7  8.7  9.0 69.2  4.2 22.2  9.2 
(...)  

I want to use apply.monthly to each of the columns, so in the end I will still have 77 columns but with monthly data instead of daily data. I tried
apply.monthly(pp, FUN=sum) but the result is a zoo object with just one column (I think is adding all the columns).

I also tried a loop:

for (i in 1:77)
{
mensal<-apply.monthly(pp[,i], FUN=sum)
}
but it also results in just one column (the 77th column). I might be able to make the loop work with some trial and error but it takes ages to compute ( I have 17897 rows and 77 columns) and I guess there is a simpler way of doing this without using loops... So if you know how, please help. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知在何时 2024-12-09 17:01:59

为了让 apply.monthly 返回具有多个列的对象,您必须使用按列操作的函数(或 apply 不按列操作的函数) 。

library(quantmod)
getSymbols("SPY")
zSPY <- as.zoo(SPY)
# sum doesn't operate by column; it sums everything to one value
sum(zSPY)
spy.sum <- apply.monthly(zSPY, sum)
# colSums operates by column
spy.colSums <- apply.monthly(zSPY, colSums)
# use apply to operate by column
spy.apply.sum <- apply.monthly(zSPY, apply, 2, sum)

In order for apply.monthly to return an object with more than one column, you have to use a function that operates by column (or apply a function that doesn't).

library(quantmod)
getSymbols("SPY")
zSPY <- as.zoo(SPY)
# sum doesn't operate by column; it sums everything to one value
sum(zSPY)
spy.sum <- apply.monthly(zSPY, sum)
# colSums operates by column
spy.colSums <- apply.monthly(zSPY, colSums)
# use apply to operate by column
spy.apply.sum <- apply.monthly(zSPY, apply, 2, sum)
饭团 2024-12-09 17:01:59

试试这个:

> library(zoo)
>
> # test data
> z <- zooreg(cbind(a = 1:365, b = 1:365), Sys.Date())
> head(z)
           a b
2011-09-02 1 1
2011-09-03 2 2
2011-09-04 3 3
2011-09-05 4 4
2011-09-06 5 5
2011-09-07 6 6
>
> aggregate(z, as.yearmon)
             a     b
Sep 2011   435   435
Oct 2011  1395  1395
Nov 2011  2265  2265
Dec 2011  3286  3286
Jan 2012  4247  4247
Feb 2012  4843  4843
Mar 2012  6107  6107
Apr 2012  6825  6825
May 2012  7998  7998
Jun 2012  8655  8655
Jul 2012  9889  9889
Aug 2012 10850 10850

Try this:

> library(zoo)
>
> # test data
> z <- zooreg(cbind(a = 1:365, b = 1:365), Sys.Date())
> head(z)
           a b
2011-09-02 1 1
2011-09-03 2 2
2011-09-04 3 3
2011-09-05 4 4
2011-09-06 5 5
2011-09-07 6 6
>
> aggregate(z, as.yearmon)
             a     b
Sep 2011   435   435
Oct 2011  1395  1395
Nov 2011  2265  2265
Dec 2011  3286  3286
Jan 2012  4247  4247
Feb 2012  4843  4843
Mar 2012  6107  6107
Apr 2012  6825  6825
May 2012  7998  7998
Jun 2012  8655  8655
Jul 2012  9889  9889
Aug 2012 10850 10850
可是我不能没有你 2024-12-09 17:01:59

这只是一个可重现的示例。创建者:

dat <- read.zoo(textConnection("date  X02R X03N X04K X04N X04R X06I X06N X08J X08P X09O X11O X12L X14N X15G X16K
 1961-01-01  8.3  5.2  3.2  0.0  8.7  5.2 15.0  7.2 11.5 13.0  0.0  4.9  0.0  2.9  6.0
 1961-01-02  1.1  3.2 10.0  0.0  0.0  3.5  0.0  8.7  0.4  1.2  0.0  0.4  0.0  3.2  0.2
 1961-01-03 12.0  4.2 50.5  0.0  9.0 38.5 15.0 31.7  1.7  8.7  9.0 69.2  4.2 22.2  9.2"), header=TRUE)

输出(数据)
结构(c(8.3, 1.1, 12, 5.2, 3.2, 4.2, 3.2, 10, 50.5, 0, 0,
0、8.7、0、9、5.2、3.5、38.5、15、0、15、7.2、8.7、31.7、11.5、
0.4、1.7、13、1.2、8.7、0、0、9、4.9、0.4、69.2、0、0、4.2、2.9、
3.2, 22.2, 6, 0.2, 9.2), .Dim = c(3L, 15L), .Dimnames = 列表(
NULL, c("X02R", "X03N", "X04K", "X04N", "X04R", "X06I", "X06N",
“X08J”、“X08P”、“X09O”、“X11O”、“X12L”、“X14N”、“X15G”、“X16K”
)), 索引 = 结构(c(-3287, -3286, -3285), 类 = "日期"), 类 = "动物园")

但现在我有办法重新创建错误,也许我可以提供经过测试的解决方案:

 ldat <-lapply(dat, FUN=apply.monthly, sum)
 as.data.frame(ldat)
           X02R X03N X04K X04N X04R X06I X06N X08J X08P X09O X11O X12L X14N X15G X16K
1961-01-03 21.4 12.6 63.7    0 17.7 47.2   30 47.6 13.6 22.9    9 74.5  4.2 28.3 15.4

看起来好像 sum 参数(最终)在位置上与 apply.monthly “FUN” 参数匹配,但是如果您命名,则会收到错误他们lapply 参数列表中都是“FUN”。

This is just for a reproducible example. Created with:

dat <- read.zoo(textConnection("date  X02R X03N X04K X04N X04R X06I X06N X08J X08P X09O X11O X12L X14N X15G X16K
 1961-01-01  8.3  5.2  3.2  0.0  8.7  5.2 15.0  7.2 11.5 13.0  0.0  4.9  0.0  2.9  6.0
 1961-01-02  1.1  3.2 10.0  0.0  0.0  3.5  0.0  8.7  0.4  1.2  0.0  0.4  0.0  3.2  0.2
 1961-01-03 12.0  4.2 50.5  0.0  9.0 38.5 15.0 31.7  1.7  8.7  9.0 69.2  4.2 22.2  9.2"), header=TRUE)

dput(dat)
structure(c(8.3, 1.1, 12, 5.2, 3.2, 4.2, 3.2, 10, 50.5, 0, 0,
0, 8.7, 0, 9, 5.2, 3.5, 38.5, 15, 0, 15, 7.2, 8.7, 31.7, 11.5,
0.4, 1.7, 13, 1.2, 8.7, 0, 0, 9, 4.9, 0.4, 69.2, 0, 0, 4.2, 2.9,
3.2, 22.2, 6, 0.2, 9.2), .Dim = c(3L, 15L), .Dimnames = list(
NULL, c("X02R", "X03N", "X04K", "X04N", "X04R", "X06I", "X06N",
"X08J", "X08P", "X09O", "X11O", "X12L", "X14N", "X15G", "X16K"
)), index = structure(c(-3287, -3286, -3285), class = "Date"), class = "zoo")

But now that I have a way to re-create the error, maybe I can offer a tested solution:

 ldat <-lapply(dat, FUN=apply.monthly, sum)
 as.data.frame(ldat)
           X02R X03N X04K X04N X04R X06I X06N X08J X08P X09O X11O X12L X14N X15G X16K
1961-01-03 21.4 12.6 63.7    0 17.7 47.2   30 47.6 13.6 22.9    9 74.5  4.2 28.3 15.4

It looks as though the sum argument (eventually) gets positionally matched to the apply.monthly "FUN" argument, but you do get an error if you name them both "FUN" in the lapply argument list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文