Consider the following:> A <- 1:10 > A[1] 1 2 3 4 5 6 7 8 9 10> dim(A)NULL> dim(A) <- c(2,5) > A[,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10> dim(A)[1] 2 5> dim(A) <- 10 > A[1] 1 2 3 4 5 6 7 8 9 10> dim(A)[1] 10 Would it not make sense to have dim(A) = length(A) for all vectors? Murray -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862
In R, vectors don't have dimensions, arrays do.> x <- c(1, 4, 5) > class(x)[1] "numeric"> y <- array(x) > class(y)[1] "array"> dim(x)NULL> dim(y)[1] 3 On Dec 4, 2007 7:35 PM, <maj at stats.waikato.ac.nz> wrote:> Consider the following: > > A <- 1:10 > > A > [1] 1 2 3 4 5 6 7 8 9 10 > > dim(A) > NULL > > dim(A) <- c(2,5) > > A > [,1] [,2] [,3] [,4] [,5] > [1,] 1 3 5 7 9 > [2,] 2 4 6 8 10 > > dim(A) > [1] 2 5 > > dim(A) <- 10 > > A > [1] 1 2 3 4 5 6 7 8 9 10 > > dim(A) > [1] 10 > > Would it not make sense to have dim(A) = length(A) for all vectors? > > Murray > -- > Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html > Department of Statistics, University of Waikato, Hamilton, New Zealand > Email: maj at waikato.ac.nz Fax 7 838 4155 > Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Wed, 5 Dec 2007, maj at stats.waikato.ac.nz wrote:> Consider the following: >> A <- 1:10 >> A > [1] 1 2 3 4 5 6 7 8 9 10 >> dim(A) > NULL >> dim(A) <- c(2,5) >> A > [,1] [,2] [,3] [,4] [,5] > [1,] 1 3 5 7 9 > [2,] 2 4 6 8 10 >> dim(A) > [1] 2 5 >> dim(A) <- 10 >> A > [1] 1 2 3 4 5 6 7 8 9 10 >> dim(A) > [1] 10 > > Would it not make sense to have dim(A) = length(A) for all vectors?No. A one-dimensional array and a vector are not the same thing. There are subtle differences, such as what names() means (see ?names). That a 1D array and a vector _print_ in the same way does occasionally lead to confusion, but then you also cannot tell from your printout that A has type "integer" and not "double". -- 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