From savicky at cs.cas.cz  Thu Sep 27 22:38:00 2007
From: savicky at cs.cas.cz (Petr Savicky)
Date: Thu, 27 Sep 2007 22:38:00 +0200
Subject: [Rd] modifying large R objects in place
In-Reply-To: <Pine.LNX.4.64.0709271502300.6922@gannet.stats.ox.ac.uk>
References: <20070926092924.GA9879@cs.cas.cz>
	<7098abec0709261052q3a6b01e2v562f9a88ed81c513@mail.gmail.com>
	<20070927131317.GB7605@cs.cas.cz>
	<Pine.LNX.4.64.0709271502300.6922@gannet.stats.ox.ac.uk>
Message-ID: <20070927203800.GA27247@cs.cas.cz>

Thank you very much for all the explanations. In particular for pointing
out that nrow is not a .Primitive unlike dim, which is the
reason for the difference in their behavior. (I rised the question
of possible bug due to this difference, not just being unsatisfied
with nrow). Also, thanks for:

On Thu, Sep 27, 2007 at 05:59:05PM +0100, Prof Brian Ripley wrote:
[...]
> 2) I expected NAMED on 'a' to be incremented by nrow(a): here is my 
> understanding.
> 
> When you called nrow(a) you created another reference to 'a' in the 
> evaluation frame of nrow.  (At a finer level you first created a promise 
> to 'a' and then dim(x) evaluated that promise, which did SET_NAMED(<SEXP>) 
> = 2.)  So NAMED(a) was correctly bumped to 2, and it is never reduced.
> 
> More generally, any argument to a closure that actually gets used will 
> get NAMED set to 2.
[...]

This explains a lot.

I appreciate also the patch to matrix by Henrik Bengtsson, which saved
me time formulating a further question just about this.

I do not know, whether there is a reason to keep nrow, ncol not .Primitive,
but if there is such, the problem may be solved by rewriting
them as follows:

nrow <- function(...) dim(...)[1]
ncol <- function(...) dim(...)[2]

At least in my environment, the new versions preserved NAMED == 1.

It has a side effect that this unifies the error messages generated
by too many arguments to nrow(x) and dim(x). Currently
  a <- matrix(1:6,nrow=2)
  nrow(a,a) # Error in nrow(a, a) : unused argument(s) (1:6)
  dim(a,a) # Error: 2 arguments passed to 'dim' which requires 1

May be, also other solutions exist.

Petr Savicky.



[Rd] modifying large R objects in place
Petr Savicky