[R] dim vs length for vectors

Olivia Lau olau at fas.harvard.edu
Fri Jan 21 06:06:02 CET 2005


Hi all,

I'm not sure if this is a feature or a bug (and I did read the 
FAQ and the posting guide, but am still not sure).  Some of my 
students have been complaining and I thought I just might ask: 
Let K be a vector of length k.  If one types dim(K), you get 
NULL rather than [1] k.  Is this logical?

Here's the way I explain it (and maybe someone can provide a 
more accurate explanation of what's going on):  R has several 
types of scalar (atomic) values, the most common of which are 
numeric, integer, logical, and character values.  Arrays are 
data structures which hold only one type of atomic value. 
Arrays can be one-dimensional (vectors), two-dimensional 
(matrices), or n-dimensional.

(We generally use arrays of n-1 dimensions to populate 
n-dimensional arrays -- thus, we generally use vectors to 
populate matrices, and matrices to populate 3-dimensional 
arrays, but could use any array of dimension < n-1 to populate 
an n-dimensional array.)

It logically follows that when one does dim() on a vector, one 
should *not* get NULL, but should get the length of the vector 
(which one *could* obtain by doing length(), but I think this is 
less logical).  I think that R should save length() for lists 
that have objects of different dimension and type.

Does this make sense?  Or is there a better explanation?

Thanks in advance!  Yours,

Olivia Lau




More information about the R-help mailing list