From murdoch at stats.uwo.ca  Mon Sep 26 15:05:47 2005
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Mon, 26 Sep 2005 09:05:47 -0400
Subject: [Rd] Assigning a zero length vector to a list (PR#8157)
In-Reply-To: <20050926113423.E6A291C699@slim.kubism.ku.dk>
References: <20050926113423.E6A291C699@slim.kubism.ku.dk>
Message-ID: <4337F22B.3070905@stats.uwo.ca>

On 9/26/2005 7:34 AM, jussi.jousimo at ktl.fi wrote:
> Full_Name: Jussi Jousimo
> Version: 2.2.0 beta
> OS: Windows XP
> Submission from: (NULL) (193.167.195.60)
> 
> 
> I'm trying to assign a zero length vector to a list:
> 
> x<-numeric()
> length(x)
> foo<-list()
> foo$bar[[1]]<-x
> length(foo$bar[[1]])
> foo
> 
> But in the list this vector turns out to be length one with random content.
> x<-character() makes R to crash.

After foo<-list(), foo$bar is NULL, so we can simplify this.

Here's a simpler version:

# These work, which is a bit of a surprise, but there is some 
inconsistency:   one x becomes a list, the other is numeric:
 > x <- NULL
 > x[[1]] <- 1:10
 > x
[[1]]
  [1]  1  2  3  4  5  6  7  8  9 10

 > x <- NULL
 > x[[1]] <- 1
 > x
[1] 1


# This generates the same bug as the above:
 > x <- NULL
 > x[[1]] <- numeric(0)
 > x
[1] 4.250083e-314

It looks like we're trying to be too clever with handling assignments to 
components of NULL.  Wouldn't it make more sense for those to generate 
an error?

Duncan Murdoch



[Rd] Assigning a zero length vector to a list (PR#8157)
Duncan Murdoch