R-alpha: R-0.15: problem with dimnames<-()

Ross Ihaka (ihaka@stat.auckland.ac.nz)
Tue, 7 Jan 1997 14:57:28 +1300 (NZDT)


From: Ross Ihaka <ihaka@stat.auckland.ac.nz>
Date: Tue, 7 Jan 1997 14:57:28 +1300 (NZDT)
Message-Id: <199701070157.OAA16344@stat13.stat.auckland.ac.nz>
To: Kurt.Hornik@ci.tuwien.ac.at
Subject: R-alpha: R-0.15:  problem with dimnames<-()
In-Reply-To: <199612301332.OAA00674@aragorn.ci.tuwien.ac.at>

Kurt Hornik writes:
 > The following problem exists:
 > 
 > R> x <- matrix(1:4, 2, 2)
 > R> x
 >      [,1] [,2]
 > [1,]    1    3
 > [2,]    2    4
 > R> dimnames(x)
 > NULL
 > R> rownames(x) <- NULL
 > R> dimnames(x)
 > [[1]]
 > NULL
 > 
 > [[2]]
 > NULL
 > R> dimnames(x)[[1]] <- NULL
 > Error: dimnames: number of dimensions must equal number of names
 > R> dimnames(x)
 > [[1]]
 > NULL
 > 
 > [[2]]
 > NULL
 > 
 > R> dimnames(x)[[2]] <- NULL
 > Error: dimnames: number of dimensions must equal number of names
 > R> dimnames(x)
 > [[1]]
 > NULL

This is a bit of a problem.  We use the rewriting rules of the S book
to transform
	dimnames(x)[[1]] <- NULL
into a sequence of assignments as follows:

	tmp <- dimnames(x)	#	tmp <- dimnames(x)
	tmp[[1]] <- NULL	#	tmp <- "[[<-"(tmp, 1, NULL)
	dimnames(x) <- tmp	#	x <- "dimnames<-"(x, tmp)

The difficulty is that "tmp" starts life as a list of length 2
and then "tmp[[1]]<-NULL" omits its first component and produces a
list of length 1.  Now "tmp" cannot be inserted back as the dimnames
attribute because it has the wrong length.

This is why I'm not totally happy about the use of x[[i]] <- NULL
to remove the i-th component of x.  It makes more sense to me to have
x[[i]] make the i-th component be NULL and to have x[i]<-NULL remove
the ith component.  This is in the spirit of "[[" manipulating things
in a list while "[" manipulates the list itself.  In S it appears that
"[<-" is a no-op for lists.

 > 
 > Hmm ... it would be nicer if there were no errors.   rownames() and
 > colnames() are `nice':
 > 
 > R> rownames(x) <- NULL
 > R> dimnames(x)
 > [[1]]
 > NULL
 > 
 > [[2]]
 > NULL
 > 
 > R> colnames(x) <- NULL
 > R> dimnames(x)
 > [[1]]
 > NULL
 > 
 > [[2]]
 > NULL
 > 
 > (I am not sure whether the `new' interpretation of NULL assignments
 > apply here or not).
 > 
 > In any case, there is at least one `real' problem with the above:
 > 
 > R> y <- matrix(rnorm(20), 10, 2)
 > R> lm(y ~ 1)
 > 
 > Call:
 > lm(formula = y ~ 1)
 > 
 > Coefficients:
 >                   [,1]      [,2]
 > (Intercept) -0.1345775 0.6649173
 > 
 > R> colnames(y) <- c("A", "B")
 > R> lm(y ~ 1)
 > Error: dimnames: number of dimensions must equal number of names
 > 
 > This comes from
 > 
 > 	dimnames(z$effects)[[1]] <- NULL
 > 
 > in the code for lm.fit() in src/library/base/funs/lm.   Replacing the
 > above by
 > 
 > 	rownames(z$effects) <- NULL
 > 
 > fixes the problem.

As does dimnames(z$effects)[1] <- LIST(NULL).  I will make a change to this.
	Ross
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- For info or help, send "info" or "help",
To [un]subscribe, send "[un]subscribe"
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-