lower.tri / upper.tri
행렬의 lower triangle 와 upper triangle 를 TRUE 나 FALSE 로 채운다
lower.tri(x, diag = FALSE)
upper.tri(x, diag = FALSE)
[인수]
1) x : 행렬
2) diag : TRUE/FALSE
[예제]
> (m2 <- matrix(1:20, 4, 5))
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
>
> lower.tri(m2)
[,1] [,2] [,3] [,4] [,5]
[1,] FALSE FALSE FALSE FALSE FALSE
[2,] TRUE FALSE FALSE FALSE FALSE
[3,] TRUE TRUE FALSE FALSE FALSE
[4,] TRUE TRUE TRUE FALSE FALSE
> upper.tri(m2)
[,1] [,2] [,3] [,4] [,5]
[1,] FALSE TRUE TRUE TRUE TRUE
[2,] FALSE FALSE TRUE TRUE TRUE
[3,] FALSE FALSE FALSE TRUE TRUE
[4,] FALSE FALSE FALSE FALSE TRUE
> m2[lower.tri(m2)]
[1] 2 3 4 7 8 12
> m2[lower.tri(m2)] <- NA
> m2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] NA 6 10 14 18
[3,] NA NA 11 15 19
[4,] NA NA NA 16 20
'프로그래밍 Programming' 카테고리의 다른 글
Data Preparation (11) - Clean (Feature Selection) (0) | 2014.12.05 |
---|---|
Data Preparation (10) - Clean (Remove the Variables) (0) | 2014.12.05 |
Correlation : cor() (0) | 2014.12.05 |
apply function (apply/lappy/sapply) (0) | 2014.12.02 |
Data Preparation (9) - Clean (Deal with Missing Values) (0) | 2014.11.29 |