[R] Correct way to test for exact dimensions of matrix or array

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Jan 10 16:03:35 CET 2006


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" <gsxej2 at cam.ac.uk>
To: <r-help at stat.math.ethz.ch>
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
>
> gsxej2 at cam.ac.uk
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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




More information about the R-help mailing list