Martin Maechler
2021-Feb-01 09:08 UTC
[Rd] surprised matrix (1:256, 8, 8) doesn't cause error/warning
>>>>> Abby Spurdle (/??bi/) >>>>> on Mon, 1 Feb 2021 19:50:32 +1300 writes:> I'm a little surprised that the following doesn't trigger an error or a warning. > matrix (1:256, 8, 8) > The help file says that the main argument is recycled, if it's too short. > But doesn't say what happens if it's too long. It's somewhat subtler than one may assume :> matrix(1:9, 2,3)[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 Warning message: In matrix(1:9, 2, 3) : data length [9] is not a sub-multiple or multiple of the number of rows [2]> matrix(1:8, 2,3)[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 Warning message: In matrix(1:8, 2, 3) : data length [8] is not a sub-multiple or multiple of the number of columns [3]> matrix(1:12, 2,3)[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6>So it looks to me the current behavior is quite on purpose. Are you sure it's not documented at all when reading the docs carefully? (I did *not*, just now).
Abby Spurdle (/əˈbi/)
2021-Feb-01 23:27 UTC
[Rd] surprised matrix (1:256, 8, 8) doesn't cause error/warning
So, does that mean that a clean result is contingent on the length of the data being a multiple of both the number of rows and columns? However, this rule is not straightforward.> #EXAMPLE 1 > #what I would expect > matrix (1:12, 0, 0)<0 x 0 matrix> Warning message: In matrix(1:12, 0, 0) : data length exceeds size of matrix> #EXAMPLE 2 > #don't like this > matrix (numeric (), 2, 3)[,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA The first example is what I would expect, but is inconsistent with the previous examples. (Because zero is a valid multiple of twelve). I dislike the second example with recycling of a zero-length vector. This *is* covered in the help file, but also seems inconsistent with the previous examples. (Because two and three are not valid multiples of zero). Also, I can't think of any reason why someone would want to construct a matrix with extra data, and then discard part of it. And even if there was, then why not allow an arbitrarily longer length? On Mon, Feb 1, 2021 at 10:08 PM Martin Maechler <maechler at stat.math.ethz.ch> wrote:> > >>>>> Abby Spurdle (/??bi/) > >>>>> on Mon, 1 Feb 2021 19:50:32 +1300 writes: > > > I'm a little surprised that the following doesn't trigger an error or a warning. > > matrix (1:256, 8, 8) > > > The help file says that the main argument is recycled, if it's too short. > > But doesn't say what happens if it's too long. > > It's somewhat subtler than one may assume : > > > matrix(1:9, 2,3) > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > Warning message: > In matrix(1:9, 2, 3) : > data length [9] is not a sub-multiple or multiple of the number of rows [2] > > > matrix(1:8, 2,3) > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > Warning message: > In matrix(1:8, 2, 3) : > data length [8] is not a sub-multiple or multiple of the number of columns [3] > > > matrix(1:12, 2,3) > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > > > > So it looks to me the current behavior is quite on purpose. > Are you sure it's not documented at all when reading the docs > carefully? (I did *not*, just now).
Hervé Pagès
2021-Feb-22 19:34 UTC
[Rd] surprised matrix (1:256, 8, 8) doesn't cause error/warning
Hi Martin, It kind of does make sense to issue the warning when **recycling** (and this is consistent with what happens with recycling in general): > matrix(1:4, 6, 6) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 3 1 3 1 3 [2,] 2 4 2 4 2 4 [3,] 3 1 3 1 3 1 [4,] 4 2 4 2 4 2 [5,] 1 3 1 3 1 3 [6,] 2 4 2 4 2 4 > matrix(1:4, 5, 6) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 2 3 4 1 2 [2,] 2 3 4 1 2 3 [3,] 3 4 1 2 3 4 [4,] 4 1 2 3 4 1 [5,] 1 2 3 4 1 2 Warning message: In matrix(1:4, 5, 6) : data length [4] is not a sub-multiple or multiple of the number of rows [5] (Note that the warning is misleading. matrix() is happy to take data with a length that is not a sub-multiple of the number of rows or cols as long as it's a sub-multiple of the length of the matrix.) However I'm not sure that **truncating** the data is desirable behavior: > matrix(1:6, 1, 3) [,1] [,2] [,3] [1,] 1 2 3 > matrix(1:6, 1, 5) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 Warning message: In matrix(1:6, 1, 5) : data length [6] is not a sub-multiple or multiple of the number of columns [5] Maybe you get a warning sometimes, if you are lucky, but still. Finally note that you never get any warning with array(): > array(1:4, c(5, 6)) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 2 3 4 1 2 [2,] 2 3 4 1 2 3 [3,] 3 4 1 2 3 4 [4,] 4 1 2 3 4 1 [5,] 1 2 3 4 1 2 > array(1:6, c(1, 5)) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 Cheers, H. On 2/1/21 1:08 AM, Martin Maechler wrote:>>>>>> Abby Spurdle (/??bi/) >>>>>> on Mon, 1 Feb 2021 19:50:32 +1300 writes: > > > I'm a little surprised that the following doesn't trigger an error or a warning. > > matrix (1:256, 8, 8) > > > The help file says that the main argument is recycled, if it's too short. > > But doesn't say what happens if it's too long. > > It's somewhat subtler than one may assume : > >> matrix(1:9, 2,3) > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > Warning message: > In matrix(1:9, 2, 3) : > data length [9] is not a sub-multiple or multiple of the number of rows [2] > >> matrix(1:8, 2,3) > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 > Warning message: > In matrix(1:8, 2, 3) : > data length [8] is not a sub-multiple or multiple of the number of columns [3] > >> matrix(1:12, 2,3) > [,1] [,2] [,3] > [1,] 1 3 5 > [2,] 2 4 6 >> > > So it looks to me the current behavior is quite on purpose. > Are you sure it's not documented at all when reading the docs > carefully? (I did *not*, just now). > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Herv? Pag?s Bioconductor Core Team hpages.on.github at gmail.com