[Rd] unlist on nested pairlists

Dan Davison davison at stats.ox.ac.uk
Wed Aug 20 18:19:13 CEST 2008


unlist(recursive=FALSE) returns NULL elements when passed a nested
pairlist containing non-NULL data:

x <- pairlist(pairlist(1:2))
unlist(x, recursive=FALSE)
## [[1]]
## NULL

version 2.7.2 RC (2008-08-18 r46382) under linux

I'm unaware of any motivation for constructing the above data
structure, but if unlist is going to operate on it without error I
would be surprised if that is the correct answer. However as I do not
know anything about pairlists I have not submitted this to r-bugs.

I'd also appreciate any comments on the following issue:

The documentation for unlist says

    recursive: logical.  Should unlisting be applied to list components of
              'x'?

Now,

y <- list(a=1:2, b=list(b1=3:4, b2=5:6))

My expectation was that unlist(y, recursive=FALSE) would be the following length 3 list:
list(a1=1, a2=2, b=list(b1=3:4, b2=5:6))

However

str(unlist(y, recursive=FALSE))
## List of 4
## $ a1  : int 1
## $ a2  : int 2
## $ b.b1: int [1:2] 3 4
## $ b.b2: int [1:2] 5 6

I'm tempted to say that, contary to the documentation, (partial)
unlisting has in fact been applied to a list component of x (x$b): it
contributes 2 components to the result, rather than one. I say partial
because the numeric vectors in x$b have not been broken up into
separate elements in the answer. However, I imagine that this is the
intended behaviour, and that my incorrect expectation is a result of
not understanding the underlying list representations. Any explanation
would be appreciated.

Dan

-- 
http://www.stats.ox.ac.uk/~davison



More information about the R-devel mailing list