[R] dim vs length for vectors
Gabor Grothendieck
ggrothendieck at myway.com
Fri Jan 21 08:29:35 CET 2005
More generally, anything that has a dim attribute is an array
including 1d, 2d, 3d structures with dim attributes.
Matrices have a dim attribute so matrices are arrays and
is.array(m) will be TRUE if m is a matrix.
miguel manese <jjonphl <at> gmail.com> writes:
:
: I think the more intuitive way to think of it is that dim works only
: for matrices (an array being a 1 column matrix). and vectors are not
: matrices.
:
: > x <- 1:5
: > class(x) # numeric
: > dim(x) <- 5
: > class(x) # array
: > dim(x) <- c(5,1)
: > class(x) # matrix
: > dim(x) <- c(1,5)
: > class(x) # matrix
:
: On Fri, 21 Jan 2005 05:35:11 +0000 (UTC), Gabor Grothendieck
: <ggrothendieck <at> myway.com> wrote:
: > 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
: >
: > ______________________________________________
: > 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
: >
:
: ______________________________________________
: 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
:
:
More information about the R-help
mailing list