如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)


問題描述

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

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 

參考解法

方法 1:

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.

(by Lisa AnnGSee)

參考文件

  1. How to apply mean, sd etc. function to a whole matrix (CC BY-SA 3.0/4.0)

#R






相關問題

如何將均值、標準差等函數應用於整個矩陣 (How to apply mean, sd etc. function to a whole matrix)

Tạo các thùng của mỗi hàng trong bảng và vẽ hình thanh ngăn xếp trong R (Make bins of each table row and draw stack bar figure in R)

Reading not quite correct .csv file in R (Reading not quite correct .csv file in R)

包'treemap'中的線條粗細 (Thickness of lines in Package ‘treemap’)

是否需要帶有 awk 的預處理文件,或者可以直接在 R 中完成? (Is preprocessing file with awk needed or it can be done directly in R?)

rpivotTable 選擇元素下拉菜單 (rpivotTable select elements drop down menu)

優化性能 - Shiny 中的大文件輸入 (Optimizing Performance - Large File Input in Shiny)

數值取決於所應用的應用系列,R (Numeric values depending of apply family applied, R)

如何記錄全年的值? (How to note the values across year?)

R中的線性搜索 (Linear search in R)

在 dplyr/purrr 工作流程中動態連接多個數據集 (Dynamically join multiple datasets in a dplyr/purrr workflow)

如何將行值更改為列名 (R) (How change Row values to Column names (R))







留言討論