Re: [R] Subsetting a list

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 21 Oct 2005 - 22:55:41 EST

On Fri, 21 Oct 2005, Martin Maechler wrote:

>>>>>> "PaCo" == Patrick Connolly <p.connolly@hortresearch.co.nz>
>>>>>> on Fri, 21 Oct 2005 13:26:08 +1300 writes:
>
> PaCo> On Wed, 19-Oct-2005 at 05:09PM +0200, Martin Maechler wrote:
> PaCo> |> Lists can have 'dim' attributes and hence be treated as arrays;
> PaCo> |> Note that this is pretty rarely used and not too well supported
> PaCo> |> by some tools, one could say even 'print()' :
> PaCo> |>
> PaCo> |> > set.seed(0); L0 <- L <- lapply(rpois(12, lambda=3), seq); dim(L) <- 3:4; L
> PaCo> |> [,1] [,2] [,3] [,4]
> PaCo> |> [1,] Integer,5 Integer,3 Integer,5 Integer,3
> PaCo> |> [2,] Integer,2 Integer,5 Integer,6 1
> PaCo> |> [3,] Integer,2 Integer,2 Integer,4 Integer,2
>
> PaCo> for an occasion such as this, it can be clearer to do:
>
> >> transform(L)
> PaCo> X1 X2 X3 X4
> PaCo> 1 1, 2, 3, 4, 5 1, 2, 3 1, 2, 3, 4, 5 1, 2, 3
> PaCo> 2 1, 2 1, 2, 3, 4, 5 1, 2, 3, 4, 5, 6 1
> PaCo> 3 1, 2 1, 2 1, 2, 3, 4 1, 2
>
> wow!
> Thank you Patrick!
>
> Actually, that's identical to
>
> data.frame(L)
>
> which I honestly wouldn't have had expected to work for L.

or even as.data.frame(L).

Now replace lambda=3 by lambda=30 and see which one is legible (let alone clearer). I prefer the internal matrix printing in almost all cases.

It works for data frames because print.data.frame uses format.data.frame which uses format, and that's how format works on a list.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Oct 21 23:07:46 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:16:03 EST