Reputation: 3485
I have the following matrix
Rho <- structure(c(1, 0.466666666666667, -0.866666666666667, -0.466666666666667,
-0.333333333333333, 0.466666666666667, 1, -0.6, -0.466666666666667,
-0.333333333333333, -0.866666666666667, -0.6, 1, 0.333333333333333,
0.466666666666667, -0.466666666666667, -0.466666666666667, 0.333333333333333,
1, -0.2, -0.333333333333333, -0.333333333333333, 0.466666666666667,
-0.2, 1), .Dim = c(5L, 5L), .Dimnames = list(c("SPX Index", "MXE2 Index",
"USG4TR Index", "FNAR Index", "DBLCMAVL Index"), c("SPX Index",
"MXE2 Index", "USG4TR Index", "FNAR Index", "DBLCMAVL Index")))
I thought that, in order to apply functions to single matrix arguments (like rows and/or column), I had to use mapply()
or apply()
in several ways.
But, if I input
mean(Rho)
sd(Rho)
this returns me separate application of functions:
> mean(Rho)
SPX Index MXE2 Index USG4TR Index FNAR Index DBLCMAVL Index
-0.04000000 0.01333333 0.06666667 0.04000000 0.12000000
> sd(Rho)
SPX Index MXE2 Index USG4TR Index FNAR Index DBLCMAVL Index
0.7566006 0.6902496 0.7774603 0.6282250 0.5932959
This is not what I want: I want the mean and st. dev. of all the elements of my matrix, like
> mean(mean(Rho))
[1] 0.04
in just one command.
Is there a way to do it without coercing my matrix to vector/numeric/other?
> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] PerformanceAnalytics_1.0.4.4 xts_0.8-6
[3] zoo_1.7-7 gogarch_0.7-2
[5] fastICA_1.1-16 fGarch_2150.81
[7] fBasics_2160.81 rmgarch_0.97
[9] Matrix_1.0-9 lattice_0.20-10
[11] Kendall_2.2 spd_1.7
[13] KernSmooth_2.23-8 rugarch_1.0-11
[15] Rsolnp_1.12 truncnorm_1.0-6
[17] chron_2.3-42 numDeriv_2012.3-1
[19] MASS_7.3-18 RcppArmadillo_0.3.4.2
[21] Rcpp_0.9.13 timeSeries_2160.94
[23] timeDate_2160.95 rcom_2.2-5
[25] rscproxy_2.0-5
loaded via a namespace (and not attached):
[1] boot_1.3-5 grid_2.15.1 stabledist_0.6-4 tools_2.15.1
Upvotes: 1
Views: 2529
Reputation: 49820
While I cannot say for sure that this is the reason you're seeing what you're seeing without seeing your sessionInfo()
, my guess is that you have the PerformanceAnalytics
package loaded.
in the zzz.R
file of that package, they have this code:
mean.xts <- function(x,...) {
if(is.vector(x) ||is.null(ncol(x)) || ncol(x)==1){
x<-as.numeric(x)
mean(x,...)
} else apply(x,2,mean.xts,...)
}
mean.matrix <- function(x,...) {apply(x,2,mean,...)}
sd.xts <- function(x,na.rm=FALSE) {
if(is.vector(x) || is.null(ncol(x)) || ncol(x)==1){
x<-as.numeric(x)
sd(x,na.rm=na.rm)
} else apply(x,2,sd,na.rm=na.rm)
}
sd.matrix <- function(x,na.rm=FALSE) {apply(x,2,sd,na.rm=na.rm)}
I think it is awful that they do that and I've tried repeatedly to get them to change it, but to no avail. Therefore, I boycott the package.
Anyway, start a fresh R session without loading that package and try again.
Upvotes: 3