> x<-matrix(1:9,nrow=3,ncol=3)
> x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> str(x)
int [1:3, 1:3] 1 2 3 4 5 6 7 8 9
> keys <- c("English","Math","Science")
> str(keys)
chr [1:3] "English" "Math" "Science"
> colnames(x) <- keys
> str(x)
int [1:3, 1:3] 1 2 3 4 5 6 7 8 9
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "English" "Math" "Science"
> keys2 <- c("a","b","c")
> rownames(x) <-keys2
> x
English Math Science
a 1 4 7
b 2 5 8
c 3 6 9
> str(x)
int [1:3, 1:3] 1 2 3 4 5 6 7 8 9
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "a" "b" "c"
..$ : chr [1:3] "English" "Math" "Science"
> x[1]
[1] 1
> x[[1]]
[1] 1
> x[,1]
a b c
1 2 3
> x["a"]
[1] NA
> x[1,"English"]
[1] 1
> x[,"English"]
a b c
1 2 3
# names()だと各要素に名前をつける
> names(x) <- keys
> x
English Math Science
a 1 4 7
b 2 5 8
c 3 6 9
attr(,"names")
[1] "English" "Math" "Science" NA NA NA NA
[8] NA NA
> str(x)
int [1:3, 1:3] 1 2 3 4 5 6 7 8 9
- attr(*, "dimnames")=List of 2
..$ : chr [1:3] "a" "b" "c"
..$ : chr [1:3] "English" "Math" "Science"
- attr(*, "names")= chr [1:9] "English" "Math" "Science" NA ...
http://cse.naro.affrc.go.jp/takezawa/r-tips/r/26.html
0 件のコメント:
コメントを投稿