Hi the list Is it possible to create an empty matrix ? I do not mean an matrix with a single value that is NA (which is not empty) but a real empty one, with length=0. I do not understand why we have length(numeric()), length(factor()) and length(character()) to zero, and length(array()) to one... Any rason for that ? Thanks Christophe
empty array
6 messages · Christophe Genolini, Gábor Csárdi, Brian Ripley +2 more
On Sat, Mar 15, 2008 at 04:33:32PM +0100, Christophe Genolini wrote:
Hi the list Is it possible to create an empty matrix ? I do not mean an matrix with a single value that is NA (which is not empty) but a real empty one, with length=0.
Sure:
matrix(nrow=0, ncol=5)
[,1] [,2] [,3] [,4] [,5]
a <- array( dim=c(5,4,0) ) dim(a)
[1] 5 4 0
length(a)
[1] 0
I do not understand why we have length(numeric()), length(factor()) and length(character()) to zero, and length(array()) to one... Any rason for that ?
That i don't know, maybe someone else does. G.
Thanks Christophe
______________________________________________ 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.
Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
On Sat, 15 Mar 2008, Gabor Csardi wrote:
On Sat, Mar 15, 2008 at 04:33:32PM +0100, Christophe Genolini wrote:
Hi the list Is it possible to create an empty matrix ? I do not mean an matrix with a single value that is NA (which is not empty) but a real empty one, with length=0.
Sure:
matrix(nrow=0, ncol=5)
[,1] [,2] [,3] [,4] [,5]
a <- array( dim=c(5,4,0) ) dim(a)
[1] 5 4 0
length(a)
[1] 0
I do not understand why we have length(numeric()), length(factor()) and length(character()) to zero, and length(array()) to one... Any rason for that ?
That i don't know, maybe someone else does.
See the reference on the help page for array and section 3.3.3 of the R FAQ (about the 16th item). When R was first developed, S did not allow zero dimensions and so the minimum length for an array (or matrix) was one.
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
Will mymatrix <- NULL do what you want?
--- Christophe Genolini <cgenolin at u-paris10.fr> wrote:
Hi the list Is it possible to create an empty matrix ? I do not mean an matrix with a single value that is NA (which is not empty) but a real empty one, with length=0. I do not understand why we have length(numeric()), length(factor()) and length(character()) to zero, and length(array()) to one... Any rason for that ? Thanks Christophe
______________________________________________ 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.
John Kane a ?crit :
Will mymatrix <- NULL do what you want?
Well, in your code, 'mymatrix' is not a matrix : a<-array(dim=c(0,0)) # Solution of Gabor Csardi <0 x 0 matrix> class(a) [1] "matrix" b<-NULL class(b) [1] "NULL" Your definition will probably works in most case, but in S4, the uses of type is an important point, so I need to use matrix where I shall use matrix, even if it is an empty one. Christophe
--- Christophe Genolini <cgenolin at u-paris10.fr> wrote:
Hi the list Is it possible to create an empty matrix ? I do not mean an matrix with a single value that is NA (which is not empty) but a real empty one, with length=0. I do not understand why we have length(numeric()), length(factor()) and length(character()) to zero, and length(array()) to one... Any rason for that ? Thanks Christophe
______________________________________________ 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.
Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail. Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca
2 days later
Hello everyone
I know other, more knowledgeable, people
have replied to Christophe's question, but
perhaps the List would be interested to know
that zero-extent arrays are useful (to me at least)
because although such an array has no content, the
dimname are nevertheless retained:
> a <- array(0,dim=c(0,3))
> dimnames(a) <- list(size=c() , fish=c("cod","skate","crab"))
> b <- array(0,dim=c(2,0))
> dimnames(b) <- list(size=c("huge","small"),depth=c())
>
We can attach these arrays ---both of which are of length 0---using
adiag():
> library(magic)
> adiag(a,b)
fish
size cod skate crab
huge 0 0 0
small 0 0 0
>
Note how the dimnames of "a" and "b" are retained in the output.
The contents of this array are the default "pad" value of adiag().
This is terribly useful in the humble workaday world
of high-dimensional magic hypercubes.
rksh
On 15 Mar 2008, at 15:33, Christophe Genolini wrote:
Hi the list Is it possible to create an empty matrix ? I do not mean an matrix with a single value that is NA (which is not empty) but a real empty one, with length=0. I do not understand why we have length(numeric()), length(factor()) and length(character()) to zero, and length(array()) to one... Any rason for that ? Thanks Christophe
______________________________________________ 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.
-- Robin Hankin Uncertainty Analyst and Neutral Theorist, National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743