jussi.jousimo@ktl.fi
2005-Sep-26 11:34 UTC
[Rd] Assigning a zero length vector to a list (PR#8157)
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.
Duncan Murdoch
2005-Sep-26 13:05 UTC
[Rd] Assigning a zero length vector to a list (PR#8157)
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
p.dalgaard@biostat.ku.dk
2005-Sep-26 14:29 UTC
[Rd] Assigning a zero length vector to a list (PR#8157)
Duncan Murdoch <murdoch at stats.uwo.ca> writes:> 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?Once upon a time, we had pairlists, and NULL was the empty list. This looks like it might be a relic. If so, it likely also predates consistent handling of zero-length vectors, so something is getting confused. I think it would be reasonable to expect similar results to this:> x<-list() > x[[1]] <- numeric(0) > x[[1]] numeric(0) S-PLUS also tries to handle NULL as a zero length list, with some anomalies:> x <- NULL > x[[1]] <- numeric(0) > x$value: numeric(0)> x <- list() > x[[1]] <- numeric(0) > x[[1]]: numeric(0) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
murdoch@stats.uwo.ca
2005-Sep-26 19:13 UTC
[Rd] Assigning a zero length vector to a list (PR#8157)
On 9/26/2005 10:29 AM, Peter Dalgaard wrote:> Duncan Murdoch <murdoch at stats.uwo.ca> writes: > >> 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? > > Once upon a time, we had pairlists, and NULL was the empty list. This > looks like it might be a relic. If so, it likely also predates > consistent handling of zero-length vectors, so something is getting > confused. I think it would be reasonable to expect similar results to > this: > >> x<-list() >> x[[1]] <- numeric(0) >> x > [[1]] > numeric(0)I agree. I think I see where the problem is (a test "length(y) <= 1" in do_subassign2_dflt in subassign.c should be "length(y) == 1"; I'll try to fix it. Duncan Murdoch> > > > S-PLUS also tries to handle NULL as a zero length list, with some > anomalies: > >> x <- NULL >> x[[1]] <- numeric(0) >> x > $value: > numeric(0) > > >> x <- list() >> x[[1]] <- numeric(0) >> x > [[1]]: > numeric(0) > >
Seemingly Similar Threads
- PS driver crashes when no permissions (PR#8078)
- Rubbish values written with zero-length vectors (PR#14217)
- Bug in out-of-bounds assignment of list object to expression() vector
- [External] Re: Bug in out-of-bounds assignment of list object to expression() vector
- Bug in list subset assignment due to NAMED optimization