[R] dim vs length for vectors

Gabor Grothendieck ggrothendieck at myway.com
Fri Jan 21 19:12:43 CET 2005



Sorry, I meant as.array, not array.


Date:   Fri, 21 Jan 2005 12:21:39 -0500 (EST) 
From:   Gabor Grothendieck <ggrothendieck at myway.com>
To:   <olau at fas.harvard.edu>, <ligges at statistik.uni-dortmund.de>, <ahenningsen at email.uni-kiel.de> 
Cc:   <r-help at stat.math.ethz.ch> 
Subject:   Re: [R] dim vs length for vectors 

 


Perhaps you could just write dim(array(x)) when you want
that effect. That has the advantage of not requiring any
change to R and preserving the meaning of dim(x) as retrieving
the dim attribute.


Date: Fri, 21 Jan 2005 10:00:01 -0500 
From: Olivia Lau <olau at fas.harvard.edu>
To: Uwe Ligges <ligges at statistik.uni-dortmund.de>,Arne Henningsen <ahenningsen at email.uni-kiel.de> 
Cc: <r-help at stat.math.ethz.ch> 
Subject: Re: [R] dim vs length for vectors 


What I was proposing doesn't require a lot of programming. Just 
whenever you call dim(), it does length() if the object is a 
vector and returns it in the format:

R> a <- 1:12
R> dim(a)
[1] 12

This means that you can provide a unified introduction to data 
structures that take only one type of atomic value (and 
generally call these structures "arrays"). What I call a "one 
dimensional array" only has one dim attribute and hence would 
use a[4] for extraction (for example), which is consistent with 
the usage for "[" (to my understanding). That way, when you 
introduce "[" and arrays at the same time, you can tell 
beginners:
1) Run dim()
2) If there's one dim, use [ ]; if there are 2 dims, use [ 
, ]; if there are 3 dims, you use [ , , ].
This is conceptually easy for a beginner and avoids a bit of 
frustration.

If I need to initiate an empty vector for example I use

R> a <- array()

It just looks like the difference between a 1-d array (which 
doesn't exist as far as I can tell) and a vector is semantic, 
and I think that R would be more logical if they were treated as 
the same thing. This doesn't mean changing the way the 
is.array(), is.vector(), <-, etc functions work, but just 
changing dim().




More information about the R-help mailing list