s-news
[Top] [All Lists]

Re: [S] Changing a list to a vector

To: s-news@wubios.wustl.edu
Subject: Re: [S] Changing a list to a vector
From: Don MacQueen <macqueen1@llnl.gov>
Date: Thu, 21 May 1998 10:08:01 -0800
Cc: Bill Venables <wvenable@attunga.stats.adelaide.edu.au>
In-reply-to: <9805192309.AA03519@attunga.stats.adelaide.edu.au>
References: <199805191704.KAA28985@shell.portal.ca> <199805191704.KAA28985@shell.portal.ca>
Sender: owner-s-news@wubios.wustl.edu
Very instructive, and thank you for mentioning it. But I haven't finished
learning yet...

> foo <- list(z=1:4, b=9:7, x=runif(3))
> do.call('c',foo)
 z1 z2 z3 z4 b1 b2 b3        x1        x2      x3
  1  2  3  4  9  8  7 0.5649553 0.4697681 0.48498
> do.call('min',foo)
[1] 0.4697681
> do.call('max',foo)
[1] 9
> do.call('range',foo)
[1] 0.4697681 9.0000000
> do.call('mean',foo)
Error in call to mean(): Argument z= not matched
Error in call to mean(): Argument b= not matched
Dumped
> mean(unlist(foo))
[1] 3.55197
>

-Don

At 3:09 PM -0800 5/19/98, Bill Venables wrote:
>Alan Donald writes:
> > This is rather a simple question that I remember running into
> > about a year ago and solving -- but I cannot remember how I
> > did it.
> >
> > I have a list whose components are numeric vectors of various
> > lengths.  What I want to do is to strip the "list" structure
> > from the object so that I get a vector consisting of all the
> > components glued together.  This is part of a program, so I
> > could -- I guess -- write a little program that trudges its
> > way through the list tacking vectors on to the end of a long
> > train of numbers.  But I think there is a function in Splus
> > that will do this for me quickly.  Searches through the Splus
> > 3.3 and 4 help files and manuals have turned up nothing.  Must
> > be looking under the wrong word.
>
>The "I could kick myself" answer is, of course,
>
>> one.big.vector <- unlist(list.of.vectors).
>
>It then occurred to me that there is another answer almost as
>simple and somewhat more instructive:
>
>> one.big.vector <- do.call("c", list.of.vectors)
>
>Bill
>-----------------------------------------------------------------------
>This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
>send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
>message:  unsubscribe s-news


--------------------------------
Don MacQueen
Environmental Protection Dept.
Lawrence Livermore National Lab
macq@llnl.gov    (925) 423-1062
--------------------------------


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>