'Using period.apply with mean function in xts gives different results (aggregated and per column)

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 .....



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1