[Rd] x <- 1:2; dim(x) <- 2? A vector or not?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jan 13 08:58:01 CET 2009


What you have is a one-dimensional array: they crop up in R most often 
from table() in my experience.

> f <- table(rpois(100, 4))
> str(f)
  'table' int [, 1:10] 2 6 18 21 13 16 13 4 3 4
  - attr(*, "dimnames")=List of 1
   ..$ : chr [1:10] "0" "1" "2" "3" ...

and yes, f is an atmoic vector and yes, str()'s notation is confusing 
here but if it did [1:10] you would not know it was an array.  I 
recall discussing this with Martin Maechler (str's author) last 
century, and I've just checked that R 2.0.0 did the same.

The place in which one-dimensional arrays differ from normal vectors 
is how names are handled: notice that my example has dimnames not 
names, and ?names says

      For a one-dimensional array the 'names' attribute really is
      'dimnames[[1]]'.

I think these days we have enough internal glue in place that an end 
user would not notice the difference (but those working at C level 
with R objects may need to know).

On Mon, 12 Jan 2009, Henrik Bengtsson wrote:

> Ran into the follow intermediate case in an external package (w/
> recent R v2.8.1 patched and R v2.9.0 devel):
>
>> x <- 1:2
>> dim(x) <- 2
>> dim(x)
> [1] 2
>> x
> [1] 1 2
>> str(x)
> int [, 1:2] 1 2
>> nrow(x)
> [1] 2
>> ncol(x)
> [1] NA
>> is.vector(x)
> [1] FALSE
>> is.matrix(x)
> [1] FALSE
>> is.array(x)
> [1] TRUE
>> x[1]
> [1] 1
>> x[,1]
> Error in x[, 1] : incorrect number of dimensions
>> x[1,]
> Error in x[1, ] : incorrect number of dimensions
>
> Is str() treating single-dimension arrays incorrectly?
>
> What does it mean to have a single dimension this way?  Should it
> equal a vector?  I am aware of "is.vector returns FALSE if x has any
> attributes except names".
>
> /Henrik
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list