Correct way to test for exact dimensions of matrix or array

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

Correct way to test for exact dimensions of matrix or array

Gregory Jefferis
Dear R Users,

I want to test the dimensions of an incoming vector, matrix or array safely
and succinctly.  Specifically I want to check if the unknown object has
exactly 2 dimensions with a specified number of rows and columns.

I thought that the following would work:

> obj=matrix(1,nrow=3,ncol=5)
> identical( dim( obj) , c(3,5) )
[1] FALSE  

But it doesn't because c(3,5) is numeric and the dims are integer.  I
therefore ended up doing something like:

> identical( dim( obj) , as.integer(c(3,5)))

OR

> isTRUE(all( dim( obj) == c(3,5) ))

Neither of which feel quite right.  Is there a 'correct' way to do this?

Many thanks,

Greg Jefferis.

PS Thinking about it, the second form is (doubly) wrong because:

> obj=array(1,dim=c(3,5,3,5))
> isTRUE(all( dim( obj) == c(3,5) ))
[1] TRUE

OR
> obj=numeric(10)
> isTRUE(all( dim( obj) == c(3,5) ))
[1] TRUE

(neither of which are equalities that I am happy with!)

--
Gregory Jefferis, PhD                               and:
Research Fellow    
Department of Zoology                               St John's College
University of Cambridge                             Cambridge
Downing Street                                      CB2 1TP
Cambridge, CB2 3EJ
United Kingdom

Tel: +44 (0)1223 336683                             +44 (0)1223 339899
Fax: +44 (0)1223 336676                             +44 (0)1223 337720

[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to test for exact dimensions of matrix or array

Dimitris Rizopoulos
you could use: isTRUE(all.equal(dim(obj), c(3, 5)))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: "Gregory Jefferis" <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, January 10, 2006 3:47 PM
Subject: [R] Correct way to test for exact dimensions of matrix or
array


> Dear R Users,
>
> I want to test the dimensions of an incoming vector, matrix or array
> safely
> and succinctly.  Specifically I want to check if the unknown object
> has
> exactly 2 dimensions with a specified number of rows and columns.
>
> I thought that the following would work:
>
>> obj=matrix(1,nrow=3,ncol=5)
>> identical( dim( obj) , c(3,5) )
> [1] FALSE
>
> But it doesn't because c(3,5) is numeric and the dims are integer.
> I
> therefore ended up doing something like:
>
>> identical( dim( obj) , as.integer(c(3,5)))
>
> OR
>
>> isTRUE(all( dim( obj) == c(3,5) ))
>
> Neither of which feel quite right.  Is there a 'correct' way to do
> this?
>
> Many thanks,
>
> Greg Jefferis.
>
> PS Thinking about it, the second form is (doubly) wrong because:
>
>> obj=array(1,dim=c(3,5,3,5))
>> isTRUE(all( dim( obj) == c(3,5) ))
> [1] TRUE
>
> OR
>> obj=numeric(10)
>> isTRUE(all( dim( obj) == c(3,5) ))
> [1] TRUE
>
> (neither of which are equalities that I am happy with!)
>
> --
> Gregory Jefferis, PhD                               and:
> Research Fellow
> Department of Zoology                               St John's
> College
> University of Cambridge                             Cambridge
> Downing Street                                      CB2 1TP
> Cambridge, CB2 3EJ
> United Kingdom
>
> Tel: +44 (0)1223 336683                             +44 (0)1223
> 339899
> Fax: +44 (0)1223 336676                             +44 (0)1223
> 337720
>
> [hidden email]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to test for exact dimensions of matrix or array

Martin Maechler
In reply to this post by Gregory Jefferis
>>>>> "Gregory" == Gregory Jefferis <[hidden email]>
>>>>>     on Tue, 10 Jan 2006 14:47:43 +0000 writes:

    Gregory> Dear R Users,

     Gregory> I want to test the dimensions of an incoming
     Gregory> vector, matrix or array safely


    Gregory> and succinctly.  Specifically I want to check if
    Gregory> the unknown object has exactly 2 dimensions with a
    Gregory> specified number of rows and columns.

    Gregory> I thought that the following would work:

    >> obj=matrix(1,nrow=3,ncol=5)
    >> identical( dim( obj) , c(3,5) )
    Gregory> [1] FALSE  

    Gregory> But it doesn't because c(3,5) is numeric and the dims are integer.  I
    Gregory> therefore ended up doing something like:

    >> identical( dim( obj) , as.integer(c(3,5)))

    Gregory> OR

    >> isTRUE(all( dim( obj) == c(3,5) ))

the last one is almost perfect if you leave a way the superfluous
isTRUE(..).

But, you say that it's part of your function checking it's
arguments.
In that case, I'd recommend

     if(length(d <- dim(obj)) != 2)
           stop("'d' must be matrix-like")
     if(!all(d == c(3,5)))
           stop("the matrix must be  3 x 5")

which also provides for nice error messages in case of error.
A more concise form with less nice error messages is

  stopifnot(length(d <- dim(obj)) == 2,
            d == c(3,50))

  ## you can leave away  all(.)  for things in stopifnot(.)




    Gregory> Neither of which feel quite right.  Is there a 'correct' way to do this?

    Gregory> Many thanks,

You're welcome,
Martin Maechler, ETH Zurich

    Gregory> Greg Jefferis.

    Gregory> PS Thinking about it, the second form is (doubly) wrong because:

    >> obj=array(1,dim=c(3,5,3,5))
    >> isTRUE(all( dim( obj) == c(3,5) ))
    Gregory> [1] TRUE

    Gregory> OR
    >> obj=numeric(10)
    >> isTRUE(all( dim( obj) == c(3,5) ))
    Gregory> [1] TRUE

    Gregory> (neither of which are equalities that I am happy with!)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to test for exact dimensions of matrix or array

Gregory Jefferis
Thanks for suggestions.  This is a simple question in principle, but there
seem to be some wrinkles - I am always having to think quite carefully about
how to test for equality in R.  I should also have said that I would like
the check to be efficient as well safe and succinct.

One suggestion was:
   
    isTRUE(all.equal(dim(obj), c(3, 5)))

But that is not so efficient because all.equal does lots of work esp if it
the objects are not equal.

Another suggestion was:

    all( dim( obj) == c(3,5) )
   
But that is not safe eg because dim(vector(10)) is NULL and
all(NULL==c(3,5)) is actually TRUE (to my initial surprise) so vectors would
pass through the net.

So, so far the only way that is efficient, safe and succinct is:

    identical( dim( obj) , as.integer(c(3,5)))

Martin Maechler pointed out that at the beginning of a function you might
want to break down the test into something less succinct, that printed more
specific error messages - a good suggestion for a top level function that is
supposed to be user friendly.

Any other suggestions?  Many thanks,

Greg Jefferis.

On 10/1/06 15:13, "Martin Maechler" <[hidden email]> wrote:

>>>>>> "Gregory" == Gregory Jefferis <[hidden email]>
>>>>>>     on Tue, 10 Jan 2006 14:47:43 +0000 writes:
>
>     Gregory> Dear R Users,
>
>      Gregory> I want to test the dimensions of an incoming
>      Gregory> vector, matrix or array safely
>
>
>     Gregory> and succinctly.  Specifically I want to check if
>     Gregory> the unknown object has exactly 2 dimensions with a
>     Gregory> specified number of rows and columns.
>
>     Gregory> I thought that the following would work:
>
>>> obj=matrix(1,nrow=3,ncol=5)
>>> identical( dim( obj) , c(3,5) )
>     Gregory> [1] FALSE
>
>     Gregory> But it doesn't because c(3,5) is numeric and the dims are
> integer.  I
>     Gregory> therefore ended up doing something like:
>
>>> identical( dim( obj) , as.integer(c(3,5)))
>
>     Gregory> OR
>
>>> isTRUE(all( dim( obj) == c(3,5) ))
>
> the last one is almost perfect if you leave a way the superfluous
> isTRUE(..).
>
> But, you say that it's part of your function checking it's
> arguments.
> In that case, I'd recommend
>
>      if(length(d <- dim(obj)) != 2)
>   stop("'d' must be matrix-like")
>      if(!all(d == c(3,5)))
>   stop("the matrix must be  3 x 5")
>
> which also provides for nice error messages in case of error.
> A more concise form with less nice error messages is
>
>   stopifnot(length(d <- dim(obj)) == 2,
>             d == c(3,50))
>
>   ## you can leave away  all(.)  for things in stopifnot(.)
>
>
>
>
>     Gregory> Neither of which feel quite right.  Is there a 'correct' way to
> do this?
>
>     Gregory> Many thanks,
>
> You're welcome,
> Martin Maechler, ETH Zurich
>
>     Gregory> Greg Jefferis.
>
>     Gregory> PS Thinking about it, the second form is (doubly) wrong because:
>
>>> obj=array(1,dim=c(3,5,3,5))
>>> isTRUE(all( dim( obj) == c(3,5) ))
>     Gregory> [1] TRUE
>
>     Gregory> OR
>>> obj=numeric(10)
>>> isTRUE(all( dim( obj) == c(3,5) ))
>     Gregory> [1] TRUE
>
>     Gregory> (neither of which are equalities that I am happy with!)
>

--
Gregory Jefferis, PhD                               and:
Research Fellow    
Department of Zoology                               St John's College
University of Cambridge                             Cambridge
Downing Street                                      CB2 1TP
Cambridge, CB2 3EJ
United Kingdom

Tel: +44 (0)1223 336683                             +44 (0)1223 339899
Fax: +44 (0)1223 336676                             +44 (0)1223 337720

[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to test for exact dimensions of matrix or array

Gabor Grothendieck
If its just succint you are after then this is slightly
shorter:

   identical(dim(x)+0, c(3,5))


On 1/10/06, Gregory Jefferis <[hidden email]> wrote:

> Thanks for suggestions.  This is a simple question in principle, but there
> seem to be some wrinkles - I am always having to think quite carefully about
> how to test for equality in R.  I should also have said that I would like
> the check to be efficient as well safe and succinct.
>
> One suggestion was:
>
>    isTRUE(all.equal(dim(obj), c(3, 5)))
>
> But that is not so efficient because all.equal does lots of work esp if it
> the objects are not equal.
>
> Another suggestion was:
>
>    all( dim( obj) == c(3,5) )
>
> But that is not safe eg because dim(vector(10)) is NULL and
> all(NULL==c(3,5)) is actually TRUE (to my initial surprise) so vectors would
> pass through the net.
>
> So, so far the only way that is efficient, safe and succinct is:
>
>    identical( dim( obj) , as.integer(c(3,5)))
>
> Martin Maechler pointed out that at the beginning of a function you might
> want to break down the test into something less succinct, that printed more
> specific error messages - a good suggestion for a top level function that is
> supposed to be user friendly.
>
> Any other suggestions?  Many thanks,
>
> Greg Jefferis.
>
> On 10/1/06 15:13, "Martin Maechler" <[hidden email]> wrote:
>
> >>>>>> "Gregory" == Gregory Jefferis <[hidden email]>
> >>>>>>     on Tue, 10 Jan 2006 14:47:43 +0000 writes:
> >
> >     Gregory> Dear R Users,
> >
> >      Gregory> I want to test the dimensions of an incoming
> >      Gregory> vector, matrix or array safely
> >
> >
> >     Gregory> and succinctly.  Specifically I want to check if
> >     Gregory> the unknown object has exactly 2 dimensions with a
> >     Gregory> specified number of rows and columns.
> >
> >     Gregory> I thought that the following would work:
> >
> >>> obj=matrix(1,nrow=3,ncol=5)
> >>> identical( dim( obj) , c(3,5) )
> >     Gregory> [1] FALSE
> >
> >     Gregory> But it doesn't because c(3,5) is numeric and the dims are
> > integer.  I
> >     Gregory> therefore ended up doing something like:
> >
> >>> identical( dim( obj) , as.integer(c(3,5)))
> >
> >     Gregory> OR
> >
> >>> isTRUE(all( dim( obj) == c(3,5) ))
> >
> > the last one is almost perfect if you leave a way the superfluous
> > isTRUE(..).
> >
> > But, you say that it's part of your function checking it's
> > arguments.
> > In that case, I'd recommend
> >
> >      if(length(d <- dim(obj)) != 2)
> >   stop("'d' must be matrix-like")
> >      if(!all(d == c(3,5)))
> >   stop("the matrix must be  3 x 5")
> >
> > which also provides for nice error messages in case of error.
> > A more concise form with less nice error messages is
> >
> >   stopifnot(length(d <- dim(obj)) == 2,
> >             d == c(3,50))
> >
> >   ## you can leave away  all(.)  for things in stopifnot(.)
> >
> >
> >
> >
> >     Gregory> Neither of which feel quite right.  Is there a 'correct' way to
> > do this?
> >
> >     Gregory> Many thanks,
> >
> > You're welcome,
> > Martin Maechler, ETH Zurich
> >
> >     Gregory> Greg Jefferis.
> >
> >     Gregory> PS Thinking about it, the second form is (doubly) wrong because:
> >
> >>> obj=array(1,dim=c(3,5,3,5))
> >>> isTRUE(all( dim( obj) == c(3,5) ))
> >     Gregory> [1] TRUE
> >
> >     Gregory> OR
> >>> obj=numeric(10)
> >>> isTRUE(all( dim( obj) == c(3,5) ))
> >     Gregory> [1] TRUE
> >
> >     Gregory> (neither of which are equalities that I am happy with!)
> >
>
> --
> Gregory Jefferis, PhD                               and:
> Research Fellow
> Department of Zoology                               St John's College
> University of Cambridge                             Cambridge
> Downing Street                                      CB2 1TP
> Cambridge, CB2 3EJ
> United Kingdom
>
> Tel: +44 (0)1223 336683                             +44 (0)1223 339899
> Fax: +44 (0)1223 336676                             +44 (0)1223 337720
>
> [hidden email]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to test for exact dimensions of matrix or array

Tony Plate-3
There's a gotcha in using identical() to compare dimensions -- it also
compares names, e.g.:

 > x <- array(1:14, dim=c(rows=3,cols=5))
 > dim(x)
rows cols
    3    5
 > identical(dim(x)+0, c(3,5))
[1] FALSE
 > identical(as.numeric(dim(x)+0), c(3,5))
[1] TRUE
 >

Gabor Grothendieck wrote:

> If its just succint you are after then this is slightly
> shorter:
>
>    identical(dim(x)+0, c(3,5))
>
>
> On 1/10/06, Gregory Jefferis <[hidden email]> wrote:
>
>>Thanks for suggestions.  This is a simple question in principle, but there
>>seem to be some wrinkles - I am always having to think quite carefully about
>>how to test for equality in R.  I should also have said that I would like
>>the check to be efficient as well safe and succinct.
>>
>>One suggestion was:
>>
>>   isTRUE(all.equal(dim(obj), c(3, 5)))
>>
>>But that is not so efficient because all.equal does lots of work esp if it
>>the objects are not equal.
>>
>>Another suggestion was:
>>
>>   all( dim( obj) == c(3,5) )
>>
>>But that is not safe eg because dim(vector(10)) is NULL and
>>all(NULL==c(3,5)) is actually TRUE (to my initial surprise) so vectors would
>>pass through the net.
>>
>>So, so far the only way that is efficient, safe and succinct is:
>>
>>   identical( dim( obj) , as.integer(c(3,5)))
>>
>>Martin Maechler pointed out that at the beginning of a function you might
>>want to break down the test into something less succinct, that printed more
>>specific error messages - a good suggestion for a top level function that is
>>supposed to be user friendly.
>>
>>Any other suggestions?  Many thanks,
>>
>>Greg Jefferis.
>>
>>On 10/1/06 15:13, "Martin Maechler" <[hidden email]> wrote:
>>
>>
>>>>>>>>"Gregory" == Gregory Jefferis <[hidden email]>
>>>>>>>>    on Tue, 10 Jan 2006 14:47:43 +0000 writes:
>>>
>>>    Gregory> Dear R Users,
>>>
>>>     Gregory> I want to test the dimensions of an incoming
>>>     Gregory> vector, matrix or array safely
>>>
>>>
>>>    Gregory> and succinctly.  Specifically I want to check if
>>>    Gregory> the unknown object has exactly 2 dimensions with a
>>>    Gregory> specified number of rows and columns.
>>>
>>>    Gregory> I thought that the following would work:
>>>
>>>
>>>>>obj=matrix(1,nrow=3,ncol=5)
>>>>>identical( dim( obj) , c(3,5) )
>>>
>>>    Gregory> [1] FALSE
>>>
>>>    Gregory> But it doesn't because c(3,5) is numeric and the dims are
>>>integer.  I
>>>    Gregory> therefore ended up doing something like:
>>>
>>>
>>>>>identical( dim( obj) , as.integer(c(3,5)))
>>>
>>>    Gregory> OR
>>>
>>>
>>>>>isTRUE(all( dim( obj) == c(3,5) ))
>>>
>>>the last one is almost perfect if you leave a way the superfluous
>>>isTRUE(..).
>>>
>>>But, you say that it's part of your function checking it's
>>>arguments.
>>>In that case, I'd recommend
>>>
>>>     if(length(d <- dim(obj)) != 2)
>>>  stop("'d' must be matrix-like")
>>>     if(!all(d == c(3,5)))
>>>  stop("the matrix must be  3 x 5")
>>>
>>>which also provides for nice error messages in case of error.
>>>A more concise form with less nice error messages is
>>>
>>>  stopifnot(length(d <- dim(obj)) == 2,
>>>            d == c(3,50))
>>>
>>>  ## you can leave away  all(.)  for things in stopifnot(.)
>>>
>>>
>>>
>>>
>>>    Gregory> Neither of which feel quite right.  Is there a 'correct' way to
>>>do this?
>>>
>>>    Gregory> Many thanks,
>>>
>>>You're welcome,
>>>Martin Maechler, ETH Zurich
>>>
>>>    Gregory> Greg Jefferis.
>>>
>>>    Gregory> PS Thinking about it, the second form is (doubly) wrong because:
>>>
>>>
>>>>>obj=array(1,dim=c(3,5,3,5))
>>>>>isTRUE(all( dim( obj) == c(3,5) ))
>>>
>>>    Gregory> [1] TRUE
>>>
>>>    Gregory> OR
>>>
>>>>>obj=numeric(10)
>>>>>isTRUE(all( dim( obj) == c(3,5) ))
>>>
>>>    Gregory> [1] TRUE
>>>
>>>    Gregory> (neither of which are equalities that I am happy with!)
>>>
>>
>>--
>>Gregory Jefferis, PhD                               and:
>>Research Fellow
>>Department of Zoology                               St John's College
>>University of Cambridge                             Cambridge
>>Downing Street                                      CB2 1TP
>>Cambridge, CB2 3EJ
>>United Kingdom
>>
>>Tel: +44 (0)1223 336683                             +44 (0)1223 339899
>>Fax: +44 (0)1223 336676                             +44 (0)1223 337720
>>
>>[hidden email]
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to test for exact dimensions of matrix or array

Martin Maechler
In reply to this post by Gabor Grothendieck
>>>>> "Gabor" == Gabor Grothendieck <[hidden email]>
>>>>>     on Tue, 10 Jan 2006 14:47:57 -0500 writes:

    Gabor> If its just succint you are after then this is slightly
    Gabor> shorter:

    Gabor> identical(dim(x)+0, c(3,5))

indeed, or, less succinct, but maybe more readable (and along the
"top-level function checks" I had proposed yesterday):

     !is.null(d <- dim(x)) && all(d == c(3,5))



    Gabor> On 1/10/06, Gregory Jefferis <[hidden email]> wrote:
    >> Thanks for suggestions.  This is a simple question in principle, but there
    >> seem to be some wrinkles - I am always having to think quite carefully about
    >> how to test for equality in R.  I should also have said that I would like
    >> the check to be efficient as well safe and succinct.
    >>
    >> One suggestion was:
    >>
    >> isTRUE(all.equal(dim(obj), c(3, 5)))
    >>
    >> But that is not so efficient because all.equal does lots of work esp if it
    >> the objects are not equal.
    >>
    >> Another suggestion was:
    >>
    >> all( dim( obj) == c(3,5) )
    >>
    >> But that is not safe eg because dim(vector(10)) is NULL and
    >> all(NULL==c(3,5)) is actually TRUE (to my initial surprise) so vectors would
    >> pass through the net.
    >>
    >> So, so far the only way that is efficient, safe and succinct is:
    >>
    >> identical( dim( obj) , as.integer(c(3,5)))
    >>
    >> Martin Maechler pointed out that at the beginning of a function you might
    >> want to break down the test into something less succinct, that printed more
    >> specific error messages - a good suggestion for a top level function that is
    >> supposed to be user friendly.
    >>
    >> Any other suggestions?  Many thanks,
    >>
    >> Greg Jefferis.
    >>
    >> On 10/1/06 15:13, "Martin Maechler" <[hidden email]> wrote:
    >>
    >> >>>>>> "Gregory" == Gregory Jefferis <[hidden email]>
    >> >>>>>>     on Tue, 10 Jan 2006 14:47:43 +0000 writes:
    >> >
    >> >     Gregory> Dear R Users,
    >> >
    >> >      Gregory> I want to test the dimensions of an incoming
    >> >      Gregory> vector, matrix or array safely
    >> >
    >> >
    >> >     Gregory> and succinctly.  Specifically I want to check if
    >> >     Gregory> the unknown object has exactly 2 dimensions with a
    >> >     Gregory> specified number of rows and columns.
    >> >
    >> >     Gregory> I thought that the following would work:
    >> >
    >> >>> obj=matrix(1,nrow=3,ncol=5)
    >> >>> identical( dim( obj) , c(3,5) )
    >> >     Gregory> [1] FALSE
    >> >
    >> >     Gregory> But it doesn't because c(3,5) is numeric and the dims are
    >> > integer.  I
    >> >     Gregory> therefore ended up doing something like:
    >> >
    >> >>> identical( dim( obj) , as.integer(c(3,5)))
    >> >
    >> >     Gregory> OR
    >> >
    >> >>> isTRUE(all( dim( obj) == c(3,5) ))
    >> >
    >> > the last one is almost perfect if you leave a way the superfluous
    >> > isTRUE(..).
    >> >
    >> > But, you say that it's part of your function checking it's
    >> > arguments.
    >> > In that case, I'd recommend
    >> >
    >> >      if(length(d <- dim(obj)) != 2)
    >> >   stop("'d' must be matrix-like")
    >> >      if(!all(d == c(3,5)))
    >> >   stop("the matrix must be  3 x 5")
    >> >
    >> > which also provides for nice error messages in case of error.
    >> > A more concise form with less nice error messages is
    >> >
    >> >   stopifnot(length(d <- dim(obj)) == 2,
    >> >             d == c(3,50))
    >> >
    >> >   ## you can leave away  all(.)  for things in stopifnot(.)
    >> >
    >> >
    >> >
    >> >
    >> >     Gregory> Neither of which feel quite right.  Is there a 'correct' way to
    >> > do this?
    >> >
    >> >     Gregory> Many thanks,
    >> >
    >> > You're welcome,
    >> > Martin Maechler, ETH Zurich
    >> >
    >> >     Gregory> Greg Jefferis.
    >> >
    >> >     Gregory> PS Thinking about it, the second form is (doubly) wrong because:
    >> >
    >> >>> obj=array(1,dim=c(3,5,3,5))
    >> >>> isTRUE(all( dim( obj) == c(3,5) ))
    >> >     Gregory> [1] TRUE
    >> >
    >> >     Gregory> OR
    >> >>> obj=numeric(10)
    >> >>> isTRUE(all( dim( obj) == c(3,5) ))
    >> >     Gregory> [1] TRUE
    >> >
    >> >     Gregory> (neither of which are equalities that I am happy with!)
    >> >
    >>
    >> --
    >> Gregory Jefferis, PhD                               and:
    >> Research Fellow
    >> Department of Zoology                               St John's College
    >> University of Cambridge                             Cambridge
    >> Downing Street                                      CB2 1TP
    >> Cambridge, CB2 3EJ
    >> United Kingdom
    >>
    >> Tel: +44 (0)1223 336683                             +44 (0)1223 339899
    >> Fax: +44 (0)1223 336676                             +44 (0)1223 337720
    >>
    >> [hidden email]
    >>
    >> ______________________________________________
    >> [hidden email] mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-help
    >> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
    >>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html