[R] dim vs length for vectors

Gabor Grothendieck ggrothendieck at myway.com
Fri Jan 21 06:35:11 CET 2005


Olivia Lau <olau <at> fas.harvard.edu> writes:

: 
: 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.
: 

In R, vectors are not arrays:

R> v <- 1:4
R> dim(v)
NULL
R> is.array(v)
[1] FALSE

R> a <- array(1:4)
R> dim(a)
[1] 4
R> is.array(a)
[1] TRUE




More information about the R-help mailing list