在XTS中使用erige.ly与平均函数应用给出不同的结果(聚合和每列)

发布于 2025-01-25 08:06:15 字数 696 浏览 1 评论 0原文

使用R和功能,我很新,所以请在您的答案中考虑到这一点。

我有数据波纹管,当我尝试使用该时期获得两个列的平均值时。与函数 + na.rm = true一起使用,因为我在真实数据中具有na,我只得到汇总均值而不是列的均值示例bellow。 问题是如何获得每列的含义并使用na.rm = true 谢谢

zoo.data_1 <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))
zoo.data_2 <- zoo(rnorm(20)+10,as.Date(13514:13744,origin="1970-01-01"))
zoo.data <- merge(zoo.data_1,zoo.data_2)
ep <- endpoints(zoo.data,'month')

period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x,na.rm = TRUE))
period.apply(zoo.data, INDEX=ep, FUN=mean)  

=“ https://i.sstatic.net/nbbix.png” alt =“在此处输入映像

“在此处输入图像说明” ..... .....

I am quite new using R and Function so please take this in account in your answer.

I have the data bellow and when I try to get the mean for both columns using the period.apply with FUNCTION + na.rm = TRUE since I have NA in the real data I get only the aggregate mean and not the column mean as in the example bellow.
The question is how can I get the per column mean and using na.rm = TRUE
Thanks

zoo.data_1 <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))
zoo.data_2 <- zoo(rnorm(20)+10,as.Date(13514:13744,origin="1970-01-01"))
zoo.data <- merge(zoo.data_1,zoo.data_2)
ep <- endpoints(zoo.data,'month')

period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x,na.rm = TRUE))
period.apply(zoo.data, INDEX=ep, FUN=mean)  

enter image description here

So what I need is format (per column) bellow but with na.rm = TRUE

enter image description here
.....

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

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

发布评论

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

评论(1

甜警司 2025-02-01 08:06:15

而不是使用lambda表达式,而是指定参数,并且应该在此处使用

zoo.data[2:5, 1] <- NA
period.apply(zoo.data, INDEX=ep, FUN= mean, na.rm = TRUE)
           zoo.data_1 zoo.data_2
2007-01-31   10.09480   10.03287
2007-02-28   10.09050   10.11955
2007-03-31   10.12363   10.05388
2007-04-30   10.17677   10.11640
2007-05-31   10.12363   10.03287
2007-06-30   10.13294   10.12142
2007-07-31   10.12363   10.05866
2007-08-19   10.00314   10.04690

,则使用基于数据的class的不同方法。

> methods('mean')
 [1] mean.Date        mean.default     mean.difftime    mean.IDate*      mean.ITime*      mean.POSIXct     mean.POSIXlt    
 [8] mean.quosure*    mean.vctrs_vctr* mean.yearmon*    mean.yearqtr*    mean.zoo*       

eange如果我们检查Mean.zoo

getAnywhere('mean.zoo')
function (x, ...) 
mean(coredata(x), ...)

因此,“ x”需要coredata的应用,这在fun中可能是不可能的,

> period.apply(zoo.data, INDEX= ep, FUN = coredata)
Error in coredata.xts(x) : currently unsupported data type
> period.apply(zoo.data, INDEX= ep, FUN = function(x) coredata(x))
Error in coredata.xts(x) : currently unsupported data type

而我们可以使用

> head(coredata(zoo.data))
     zoo.data_1 zoo.data_2
[1,]   9.373546   9.897212
[2,]         NA  10.387672
[3,]         NA   9.946195
[4,]         NA   8.622940
[5,]         NA   9.585005
[6,]   9.179532   9.605710

Instead of using a lambda expression, specify the argument and it should work

zoo.data[2:5, 1] <- NA
period.apply(zoo.data, INDEX=ep, FUN= mean, na.rm = TRUE)
           zoo.data_1 zoo.data_2
2007-01-31   10.09480   10.03287
2007-02-28   10.09050   10.11955
2007-03-31   10.12363   10.05388
2007-04-30   10.17677   10.11640
2007-05-31   10.12363   10.03287
2007-06-30   10.13294   10.12142
2007-07-31   10.12363   10.05866
2007-08-19   10.00314   10.04690

Here, the mean use a different method based on the class of the data i.e.

> methods('mean')
 [1] mean.Date        mean.default     mean.difftime    mean.IDate*      mean.ITime*      mean.POSIXct     mean.POSIXlt    
 [8] mean.quosure*    mean.vctrs_vctr* mean.yearmon*    mean.yearqtr*    mean.zoo*       

If we check mean.zoo,

getAnywhere('mean.zoo')
function (x, ...) 
mean(coredata(x), ...)

Therefore, the 'x' requires application of coredata, which may not be possible within the FUN

> period.apply(zoo.data, INDEX= ep, FUN = coredata)
Error in coredata.xts(x) : currently unsupported data type
> period.apply(zoo.data, INDEX= ep, FUN = function(x) coredata(x))
Error in coredata.xts(x) : currently unsupported data type

whereas we can use

> head(coredata(zoo.data))
     zoo.data_1 zoo.data_2
[1,]   9.373546   9.897212
[2,]         NA  10.387672
[3,]         NA   9.946195
[4,]         NA   8.622940
[5,]         NA   9.585005
[6,]   9.179532   9.605710
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文