s-news
[Top] [All Lists]

Re: convertion of result of tapply() to data frame

To: Vadim Ogranovich <vograno@arbitrade.com>
Subject: Re: convertion of result of tapply() to data frame
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
Date: Sat, 28 Jul 2001 07:55:26 +0100 (BST)
Cc: <s-news@lists.biostat.wustl.edu>
In-reply-to: <AFD78192EC49D311BFAE00902798AB8F0BE06D@jupiter.sc.arbitrade.com>
On Fri, 27 Jul 2001, Vadim Ogranovich wrote:

> Dear S-Users, I tapply "summary" function to a vector and get back a list of
> vectors of length six as a result. So far so good. Then I want to convert
> this list to a data frame (actually to a matrix)and instead of getting a 2*6
> data frame I have a 1*12 one. Any suggestion why? Thanks, Vadim

> class(l[[1]])
[1] "table"
> dim(l[[1]])
[1] 1 6

which also explains the printout as characters.  You should expect 6x2, I
think, no 2x6.

l <- tapply(x, f, function(x) as.vector(summary(x)))
$FALSE:
[1]  2.0  6.5 11.0 11.0 15.5 20.0

$TRUE:
[1]  1.0  5.5 10.0 10.0 14.5 19.0

> data.frame(l)
  FALSE TRUE
1   2.0  1.0
2   6.5  5.5
3  11.0 10.0
4  11.0 10.0
5  15.5 14.5
6  20.0 19.0

(I'd use as.data.frame, BTW.)   I'll leave you to work out how to preserve
the names.


>
>
> > x <- seq(1,20)
> > f <- rep(c(T,F), 10)
> > l <- tapply(x, f, summary)
> > l
> $FALSE:
>    Min. 1st Qu. Median   Mean 3rd Qu.   Max.
>  " 2.0"  " 6.5" "11.0" "11.0"  "15.5" "20.0"
>
> $TRUE:
>    Min. 1st Qu. Median   Mean 3rd Qu.   Max.
>  " 1.0"  " 5.5" "10.0" "10.0"  "14.5" "19.0"
>
> > data.frame(l)
>  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
>     2  6.5   11   11 15.5   20    1  5.5   10    10  14.5    19
> > mode(l)
> [1] "list"
> > class(l)
> [1] "named"
>
>
> --------------------------------------------------
> DISCLAIMER
> This e-mail, and any attachments thereto, is intended only for use by the
> addressee(s) named herein and may contain legally privileged and/or
> confidential information.  If you are not the intended recipient of this
> e-mail, you are hereby notified that any dissemination, distribution or
> copying of this e-mail, and any attachments thereto, is strictly prohibited.
> If you have received this e-mail in error, please immediately notify me and
> permanently delete the original and any copy of any e-mail and any printout
> thereof.
>
> E-mail transmission cannot be guaranteed to be secure or error-free.  The
> sender therefore does not accept liability for any errors or omissions in
> the contents of this message which arise as a result of e-mail transmission.
>
> NOTICE REGARDING PRIVACY AND CONFIDENTIALITY
>
> Knight Trading Group may, at its discretion, monitor and review the content
> of all e-mail communications.
>
> ---------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
>

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


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