2014-09-24 72 views
0

我有一個名爲pcaObj的「prcomp」對象。如何確定R中prcomp對象的小數位數

當我做類(pcaObj),我得到 -

[1] "prcomp" 

當我做STR(pcaObj),我得到 -

List of 5 
$ sdev : num [1:10] 1.834 1.333 1.079 0.919 0.843 ... 
$ rotation: num [1:10, 1:10] -0.279 0.447 0.271 0.375 0.279 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:10] "Climate" "Diversions" "Economic" "Education" ... 
    .. ..$ : chr [1:10] "PC1" "PC2" "PC3" "PC4" ... 
$ center : Named num [1:10] 63.7 41.9 35.1 38.6 47.6 ... 
    ..- attr(*, "names")= chr [1:10] "Climate" "Diversions" "Economic" "Education" ... 
$ scale : Named num [1:10] 9.93 13.36 8.44 14.09 11.92 ... 
    ..- attr(*, "names")= chr [1:10] "Climate" "Diversions" "Economic" "Education" ... 
$ x  : num [1:193, 1:10] -2.77 -1.08 -3.17 -2.13 -3.15 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:193] "1.Albertville.AL" "2.Auburn-Opelika.AL" "3.Cullman.AL" "4.Selma.AL" ... 
    .. ..$ : chr [1:10] "PC1" "PC2" "PC3" "PC4" ... 
- attr(*, "class")= chr "prcomp" 

然後我做總結(pcaObj),並獲得類似的東西 -

summary(pcaObj) 
Importance of components: 
          PC1 PC2 PC3  PC4  PC5  PC6  PC7 
Standard deviation  1.8336 1.3328 1.0788 0.91905 0.84344 0.80628 0.75001 
Proportion of Variance 0.3362 0.1776 0.1164 0.08447 0.07114 0.06501 0.05625 
Cumulative Proportion 0.3362 0.5138 0.6302 0.71469 0.78583 0.85084 0.90709 

但是,我希望彙總函數中列出的值在每個時間段後都精確地爲3位小數。我試過的東西的sprintf( 「%3F」,摘要(不公開(pcaObj))) -

[1] "-3.842" "-0.526" "0.012" "0.307" "0.516" "65.800" 

請如果你能幫助!我很抱歉,我不確定如何在這種情況下重現此對象!

編輯 -

我試過了@MrFlick的建議。首先,我重寫功能,然後我把它叫做我的對象,但我得到的錯誤:

getSum <- function (object, ...) 
{ 
    vars <- object$sdev^2 
    vars <- vars/sum(vars) 
    importance <- rbind(`Standard deviation` = sprintf("%.3f", summary(object$sdev)), `Proportion of Variance` = sprintf("%.3f", summary(vars)), `Cumulative Proportion` = sprintf("%.3f", summary(cumsum))) 
    colnames(importance) <- colnames(object$rotation) 
    object$importance <- importance 
    class(object) <- "summary.prcomp" 
    object 
} 

getSum(pcaObj) 

的對象錯誤[我]:「內置」類型的對象不是subsettable

我也嘗試使用圓形和小數,但仍然只有一些列的改變!:

getSum <- function (object, ...) 
{ 
    vars <- object$sdev^2 
    vars <- vars/sum(vars) 
    importance <- rbind(`Standard deviation` = object$sdev, `Proportion of Variance` = round(vars, 
     digits = 3), `Cumulative Proportion` = round(cumsum(vars), digits = 3)) 
    colnames(importance) <- colnames(object$rotation) 
    object$importance <- importance 
    class(object) <- "summary.prcomp" 
    object 
} 

getSum(pcaObj) 

Importance of components: 
         PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8 PC9 PC10 
Standard deviation  1.834 1.333 1.079 0.9191 0.8434 0.8063 0.750 0.6346 0.5623 0.4585 
Proportion of Variance 0.336 0.178 0.116 0.0840 0.0710 0.0650 0.056 0.0400 0.0320 0.0210 
Cumulative Proportion 0.336 0.514 0.630 0.7150 0.7860 0.8510 0.907 0.9470 0.9790 1.0000 

正如你可以看到,一些列的3人是4位小數。

+1

你可以嘗試'選項(位數= 4)'設置全局選項。或者'print(summary(towns.pca),digits = 4)'可能有效。但'sprintf'會將所有的值轉換爲字符,你可能不想要 – 2014-09-24 03:42:11

+1

舍入被硬編碼到'summary.prcomp'函數中(參見'getAnywhere(summary.prcomp)')。 'summary()'只是爲了在屏幕上看起來很漂亮。如果您將它用於其他目的,最好提取並格式化所需的值。如果你願意,你可以根據自己的具體需要調整代碼(就像8行)並創建自己的彙總功能。 – MrFlick 2014-09-24 03:44:39

+1

它似乎可以改變某些值,但。我有幾列在'print(summary(prcomp(mtcars)),digits = 2)' – 2014-09-24 03:49:07

回答

0

這裏的功能的更新版本,您試圖重新寫

getSum <- function (object, strf="%.3f", ...) { 
    vars <- object$sdev^2 
    vars <- vars/sum(vars) 
    strf <- rep_len(strf, 3) 
    importance <- rbind(
     `Standard deviation` = sprintf(strf[1], object$sdev), 
     `Proportion of Variance` = sprintf(strf[2], vars), 
     `Cumulative Proportion` = sprintf(strf[3], cumsum(vars))) 
    colnames(importance) <- colnames(object$rotation) 
    object$importance <- noquote(importance) 
    class(object) <- "summary.prcomp" 
    object 
} 

你剛纔複製在計算時犯了一些錯誤,你增加了一些不必要的summary()電話。如果你喜歡,我也提供給你,可以指定格式作爲參數。現在這似乎工作

px<-prcomp(USArrests, scale = TRUE) 
getSum(px) 

# Importance of components: 
#      PC1 PC2 PC3 PC4 
# Standard deviation  1.575 0.995 0.597 0.416 
# Proportion of Variance 0.620 0.247 0.089 0.043 
# Cumulative Proportion 0.620 0.868 0.957 1.000