[R] strange behavior of matrix
Phil Spector
spector at stat.berkeley.edu
Tue Apr 6 02:08:29 CEST 2010
William -
An interesting feature of matrix indexing in R is that
if you provide a two column matrix as a subscript, you are
refering to the elements whose indices are in the rows
of the matrix. This is extremely handy for converting
tables to matrices:
> m = cbind(c(1,1,1,2,2,2,3,3,3),c(1,2,3,1,2,3,1,2,3),c(10,19,8,14,12,6,17,9,2))
> m
[,1] [,2] [,3]
[1,] 1 1 10
[2,] 1 2 19
[3,] 1 3 8
[4,] 2 1 14
[5,] 2 2 12
[6,] 2 3 6
[7,] 3 1 17
[8,] 3 2 9
[9,] 3 3 2
> newmat = matrix(0,3,3)
> newmat[m[,1:2]] = m[,3]
> newmat
[,1] [,2] [,3]
[1,] 10 19 8
[2,] 14 12 6
[3,] 17 9 2
It's also handy for extracting a vector with just the elements you want:
> newmat[cbind(c(1,2,3),c(2,3,1))]
[1] 19 6 17 # 1,2 2,3 3,1 elements
So it's a bit surprising when you index a matrix with a 2 column
matrix, but it is a documented fact.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Mon, 5 Apr 2010, William Revelle wrote:
> Dear R list,
>
> I have discovered a seemingly peculiar feature when using a matrix to index
> itself (yes, this is strange code, which I have now modified to be more
> reasonable).
>
> #this makes sense
> s <- matrix(1:3,nrow=1)
> s[s] #all three elements are shown
>
> #but when I try
> s <- matrix(1:2,nrow=1)
> s[1] #fine, the first element is shown
> s[2] #fine, the second element is shown
> s[s] #just the second element is shown -- this is peculiar
>
>
> #But doing it by columns works for both cases
> s <- matrix(1:3,ncol=1)
> s[s] #all three elements are shown
>
>
> #and when I try the same problem down a column
> s <- matrix(1:2,ncol=1)
> s[1] #fine
>
> s[2] #fine
>
> s[s] #this shows both elements as would be expected
>
> #clearly since I have just one dimension, it would have been better to
> s <- 1:2
> s[s] #which works as one would expect.
>
> Or, using the array function we get the same problem.
>
>> s <- array(1:2,dim=c(1,2))
>> s[s]
> [1] 2
>> s <- array(1:2,dim=c(2,1))
>> s[s]
> [1] 1 2
>
>
>> sessionInfo()
> R version 2.11.0 Under development (unstable) (2010-03-24 r51389)
> i386-apple-darwin9.8.0
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] psych_1.0-87
>>
>
> I think this is unexpected behavior.
>
> Best wishes,
>
> Bill
>
>
> --
> William Revelle http://personality-project.org/revelle.html
> Professor http://personality-project.org
> Department of Psychology http://www.wcas.northwestern.edu/psych/
> Northwestern University http://www.northwestern.edu/
> Use R for psychology http://personality-project.org/r
> It is 6 minutes to midnight http://www.thebulletin.org
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list