stefan.albrecht@allianz.com
2005-Oct-18 12:21 UTC
[Rd] Inconsistency with matrix indexing (PR#8214)
Full_Name: Stefan Albrecht Version: 2.2.0 OS: Windows XP Submission from: (NULL) (194.127.2.74) Dear all, in the new R 2.2.0, the matrix indexing has been changed. In my opinion, this leads to an inconsistency when indexing one-row matrices. Take> (mat <- matrix(1:9, nrow = 3, dimnames = list(paste("r", 1:3, sep = ""),paste("c", 1:3, sep = "")))) c1 c2 c3 r1 1 4 7 r2 2 5 8 r3 3 6 9 Now taking the first row of the first-row of mat preserves the column names> mat[1, , drop = FALSE][1, ]c1 c2 c3 1 4 7 This is not true for the first column of the first-row of mat> mat[1, , drop = FALSE][, 1][1] 1 However, for the first column of the first two rows of mat the row names are retained> mat[1:2, , drop = FALSE][, 1]r1 r2 1 2 This feature can lead to problems when indexing matrixes of different row numbers, since the row names are not preserved in all cases. I suggest to preseve the row names also for> mat[1, , drop = FALSE][, 1]Would it be possible to implement this feature? Many thanks. With best regards, Stefan Albrecht
stefan.albrecht at allianz.com wrote:> Full_Name: Stefan Albrecht > Version: 2.2.0 > OS: Windows XP > Submission from: (NULL) (194.127.2.74) > > > Dear all, > > in the new R 2.2.0, the matrix indexing has been changed.I just tried your code in R 2.0.1 and 2.1.1, and got the same output as you did. Which version are you comparing to? Duncan Murdoch In my opinion, this> leads to an inconsistency when indexing one-row matrices. > Take > >>(mat <- matrix(1:9, nrow = 3, dimnames = list(paste("r", 1:3, sep = ""), > > paste("c", 1:3, sep = "")))) > c1 c2 c3 > r1 1 4 7 > r2 2 5 8 > r3 3 6 9 > > Now taking the first row of the first-row of mat preserves the column names > >>mat[1, , drop = FALSE][1, ] > > c1 c2 c3 > 1 4 7 > > This is not true for the first column of the first-row of mat > >>mat[1, , drop = FALSE][, 1] > > [1] 1 > > However, for the first column of the first two rows of mat the row names are > retained > >>mat[1:2, , drop = FALSE][, 1] > > r1 r2 > 1 2 > > This feature can lead to problems when indexing matrixes of different row > numbers, since the row names are not preserved in all cases. > > I suggest to preseve the row names also for > >>mat[1, , drop = FALSE][, 1] > > > Would it be possible to implement this feature? Many thanks. > > With best regards, > > Stefan Albrecht > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
maechler@stat.math.ethz.ch
2005-Oct-19 12:08 UTC
[Rd] Inconsistency with matrix indexing (PR#8214)
>>>>> "Duncan" == Duncan Murdoch <murdoch at stats.uwo.ca> >>>>> on Tue, 18 Oct 2005 09:50:59 -0400 writes:Duncan> On 10/18/2005 9:32 AM, stefan.albrecht at allianz.com wrote: >> Dear Duncan, >> >> you are right. In both R 2.2.0 and 2.1.1 you get the same result. What has >> actually changed is the following: >> In R 2.1.1 >>> mat[1, , drop = FALSE][1] >> r1 >> 1 >> >> While in R 2.2.0 >>> mat[1, , drop = FALSE][1] >> [1] 1 Duncan> I don't agree with your suggestion. mat[1, , drop = FALSE] Duncan> is a 1x3 matrix, with both row and column Duncan> names. Indexing it by [1] says that you want to Duncan> treat it as a vector, and give the first element. Duncan> How could R know whether to keep the row or column Duncan> name for that [1,1] element? What should it do with Duncan> the expression Duncan> mat[1,1 , drop = FALSE][1] Duncan> ? Duncan> If you are really concerned about the names, you should use matrix Duncan> indexing in both places with drop = FALSE: >> mat[1, , drop = FALSE][1,1,drop=FALSE] Duncan> c1 Duncan> r1 1 Duncan> Duncan Murdoch yes, definitely. There's no bug here, and there was rather a bug in previous behavior. Just a simpler example {"for posteriority"}:> (M <- cbind(a=0:1, b=c(A=2, B=3)))a b A 0 2 B 1 3> M[1,2, drop=FALSE]b A 2> M[1,2][1] 2> M[2] ### treat M as vector -> lose names[1] 1> M[2:3][1] 1 2>