Search code examples
rxtsquantmod

monthlyReturn for multivariate XTS


I have an XTS called AUM which has 5 columns

head(AUM)
             FXI.Adjusted X2823.HK.Adjusted HSCE.Adjusted X3049.HK.Adjusted HSI.Adjusted
2010-04-08     100.0000          100.0000      100.0000          100.0000     100.0000
2010-04-09     100.9009          100.9009      100.9009          100.9009     100.9009
2010-04-12     100.4474          100.6022      100.1916          100.1448     100.0890
2010-04-13     101.7668          101.8123      101.6008          101.4299     101.4111
2010-04-14     102.1177          102.3792      101.9873          101.7272     101.7813
2010-04-15     102.0445          102.1387      101.7266          101.5462     101.4893

I want to calculate monthly returns for each column. I can do it for one column by calling monthlyReturn(AUM[,1]) but I cannot get it calculate monthly returns for all five columns and return a multivariate XTS.

I tried this apply(AUM, 2, monthlyReturn) but I get the error

Error in array(r, dim = d, dimnames = if (!(is.null(n1 <- names(x[[1L]])) &  : 
  length of 'dimnames' [1] not equal to array extent

Any help much appreciated. Thanks in advanced.


Solution

  • It should work with the function lapply:

    lapply(AUM, monthlyReturn)
    

    This command will return a list of xts objects.