Problem with the matrix() function.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with the matrix() function.

Rolf Turner

In the course of writing a bit of somewhat convoluted code I recently
made a silly error that revealed the following phenomenon:

m <- matrix(1:10,nrow=2,ncol=c(5,4))

produces

>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    3    5    7    9
> [2,]    2    4    6    8   10

That is, the nonsense value of c(5,4) for the "ncol" argument is
accepted, without comment --- the first entry of the given ncol argument
is used.

It might be argued that this is a reasonable accommodation of the user's
ineptitude.  I am of the opinion that an error should be thrown if the
value of ncol is not an integer scalar.

I have also discerned that if ncol is not an integer, it is replaced by
its floor value.

Is this a Good Thing?

What do others think?

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with the matrix() function.

Bert Gunter-2
I have no opinion on your queries, but whatever is done, it should be
properly documented, which appears not to be the case presently afaics.

-- Bert



On Wed, Nov 7, 2018 at 12:33 PM Rolf Turner <[hidden email]> wrote:

>
> In the course of writing a bit of somewhat convoluted code I recently
> made a silly error that revealed the following phenomenon:
>
> m <- matrix(1:10,nrow=2,ncol=c(5,4))
>
> produces
>
> >      [,1] [,2] [,3] [,4] [,5]
> > [1,]    1    3    5    7    9
> > [2,]    2    4    6    8   10
>
> That is, the nonsense value of c(5,4) for the "ncol" argument is
> accepted, without comment --- the first entry of the given ncol argument
> is used.
>
> It might be argued that this is a reasonable accommodation of the user's
> ineptitude.  I am of the opinion that an error should be thrown if the
> value of ncol is not an integer scalar.
>
> I have also discerned that if ncol is not an integer, it is replaced by
> its floor value.
>
> Is this a Good Thing?
>
> What do others think?
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with the matrix() function.

David Carlson
There are other functions where R handles a vector argument differently. The first value may be silently used, used with a warning, or trigger an error.

> x <- 1:3
> x:9
[1] 1 2 3 4 5 6 7 8 9
Warning message:
In x:9 : numerical expression has 3 elements: only the first used
> seq(x, 9)
Error in seq.default(x, 9) : 'from' must be of length 1
> rep(1, x)
Error in rep(1, x) : invalid 'times' argument

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <[hidden email]> On Behalf Of Bert Gunter
Sent: Wednesday, November 7, 2018 2:49 PM
To: Rolf Turner <[hidden email]>
Cc: R-help <[hidden email]>
Subject: Re: [R] Problem with the matrix() function.

I have no opinion on your queries, but whatever is done, it should be
properly documented, which appears not to be the case presently afaics.

-- Bert



On Wed, Nov 7, 2018 at 12:33 PM Rolf Turner <[hidden email]> wrote:

>
> In the course of writing a bit of somewhat convoluted code I recently
> made a silly error that revealed the following phenomenon:
>
> m <- matrix(1:10,nrow=2,ncol=c(5,4))
>
> produces
>
> >      [,1] [,2] [,3] [,4] [,5]
> > [1,]    1    3    5    7    9
> > [2,]    2    4    6    8   10
>
> That is, the nonsense value of c(5,4) for the "ncol" argument is
> accepted, without comment --- the first entry of the given ncol argument
> is used.
>
> It might be argued that this is a reasonable accommodation of the user's
> ineptitude.  I am of the opinion that an error should be thrown if the
> value of ncol is not an integer scalar.
>
> I have also discerned that if ncol is not an integer, it is replaced by
> its floor value.
>
> Is this a Good Thing?
>
> What do others think?
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.