pcaObj라는 "prcomp"개체가 있습니다. I 클래스 (pcaObj)을 수행 할 때prcomp 개체의 소수 자릿수를 결정하는 방법
, 내가 얻을 -
[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 자리수가되도록하고 싶습니다. 당신이 할 수있는 경우
[1] "-3.842" "-0.526" "0.012" "0.307" "0.516" "65.800"
도와주세요 - 나는 sprintf를 (". % 3F", 요약 (올라가지는 (pcaObj))) 같은 것들을 시도! 이 경우이 객체를 재현하는 방법을 모르겠습니다.
편집 -
@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 개 소수입니다 수 있습니다! : 변경됩니다.
전역 옵션을 설정하려면'options (digits = 4) '를 시도해보십시오. 또는'print (summary (towns.pca), digits = 4)'가 작동 할 수도 있습니다. 그러나'sprintf'는 모든 값을 문자로 변환합니다. 아마도 원하지 않을 것입니다. –
반올림은'summary.prcomp' 함수에 하드 코딩되어 있습니다 ('getAnywhere (summary.prcomp)'참고). 'summary()'는 단지 화면에서보기 좋게하기위한 것입니다. 다른 용도로 사용하는 경우 원하는 값을 추출하고 형식을 지정하는 것이 좋습니다. 원한다면 특정 요구에 맞게 (8 줄과 같은) 해당 코드를 적용하고 자신의 요약 기능을 만들 수 있습니다. – MrFlick
특정 값으로 변경할 수있는 것 같습니다. 'print (summary (prcomp (mtcars)), digits = 2) '에서 바꿀 컬럼이 몇개 있습니다. –