From murdoch at stats.uwo.ca  Tue Jul 17 12:15:29 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue, 17 Jul 2007 06:15:29 -0400
Subject: [Rd] Field initialization order bug?
In-Reply-To: <p0624081dc2c19b42aa26@[128.165.72.98]>
References: <p0624081dc2c19b42aa26@[128.165.72.98]>
Message-ID: <469C96C1.30607@stats.uwo.ca>

Daniel Wilhelm wrote:
> I believe that I may have found a bug in R. The top code sample gives 
> an error as shown. However, by simply switching which field is 
> initialized first as in the bottom code sample, it works as expected.
>
>
> This gives an error:
>
>
> a <- NULL
> a[["field1"]] <- 1
> a[["field2"]] <- matrix(c(2,1), 1)
>
> Error in a[["field2"]] <- matrix(c(2, 1), 1) :
> 	more elements supplied than there are to replace
>
>
>
> Yet, this works as expected:
>
> a <- NULL
> a[["field2"]] <- matrix(c(2,1), 1)
> a[["field1"]] <- 1
>   
I'm surprised any of these work.  I didn't expect to be able to index 
NULL, but the clue is there in the man page for "[[":  "the 
left-hand-side is coerced as needed to accept the values."

What's happening is that in the first case, a becomes a numeric vector, 
and you can't assign a matrix to an element of a numeric vector.  It 
won't fit.

In the second case, a  becomes a list, and the assignments both succeed.

Duncan Murdoch



[Rd] Field initialization order bug?
Duncan Murdoch