Re: [S] Efficient method for producing a data frame of summary statistics

John Wallace (jrw@fish.washington.edu)
Fri, 24 Apr 1998 14:19:45 -0700 (PDT)


Frank,

This is the flip side of my agg.table() post. Use aggregate(), it leaves
everything as a dataframe. Then if you want to see a table, use
agg.table().

-John

-------------

>From jrw@fish.washington.eduFri Apr 24 14:06:15 1998
Date: Wed, 8 Apr 1998 15:01:48 -0700 (PDT)
From: John Wallace <jrw@fish.washington.edu>
To: Paul Schwarz <Paul.Schwarz@cornell.edu>
Cc: S-news <s-news@wubios.wustl.edu>
Subject: Re: [S] data frame manipulation

tapply() does work, but to stick with aggregate() use agg.table() below.

> attach(barley)
> agg.table(aggregate(list(Mean.of.Barley=barley[,1]),
list(Year=year,City=site), mean))

Mean.of.Barley

Year City

Crookston Duluth Grand Rapids Morris University Farm Waseca
1931 43.66 30.29333 29.05334 29.28667 35.82667 54.34667
1932 31.18 25.70000 20.81000 41.51333 29.50667 41.87000

Or for just a quick look:

agg.table(aggregate( barley[,1], list(year,site), mean ))

x

Group.1 Group.2

Crookston Duluth Grand Rapids Morris University Farm Waseca
1931 43.66 30.29333 29.05334 29.28667 35.82667 54.34667
1932 31.18 25.70000 20.81000 41.51333 29.50667 41.87000

-John

P.S. Using ["Mean of Barley"=] in the first example results in the same
output.

---------------

"agg.table" <- function(df)
{
#
# DATE WRITTEN: 1997
# AUTHOR: John R. Wallace (jw@u.washington.edu)
#
labels <- dimnames(df)[[2]]
row <- sort(unique(df[[1]]))
col <- sort(unique(df[[2]]))
row.len <- length(row)
col.len <- length(col)
out <- matrix(NA, nrow = row.len, ncol = col.len, dimnames =
list(as.character(row),
as.character(col)))
for(i in 1:row.len) {
for(j in 1:col.len) {
out[i, j] <- df[df[[1]] == row[i] & df[[2]] ==
col[j], 3]
}
}
cat("\n\t\t", labels[3], "\n\n", labels[1], "\t\t\t", labels[2], "\n\n")
out
}

---------------------------

On Wed, 8 Apr 1998, Paul Schwarz wrote:

>
> I'm developing my data manipulation skills in S-PLUS, and I've encountered
> the following problem. Using the "barley" data frame as an example, I'd
> like to create a new data frame containing the year by site mean yields
> where years are the rows and sites are the columns.
>
> I know how to calculate the summary data using the aggregate function:
>
> > aggregate( barley[,1], list(year,site), mean )
>
> This creates a data frame that looks like the following:
>
> Group.1 Group.2 x
> 1 1932 Grand Rapids 20.81000
> 2 1931 Grand Rapids 29.05334
> 3 1932 Duluth 25.70000
> 4 1931 Duluth 30.29333
> 5 1932 University Farm 29.50667
> 6 1931 University Farm 35.82667
> .
> .
> .
>
> What I want end up with, though, is something that looks like this:
>
> Year 'Grand Rapids' 'Duluth' 'University Farm' ...
> 1931 29.05334 30.29333 35.82667 ...
> 1932 20.81000 25.70000 29.50667 ...
>
> I'm not sure how to create this last data frame. Any suggestions?
>
> Thanks again!
>
> -Paul
> Paul.Schwarz@Cornell.EDU
>
> P.S. Until I am re-subscribed to s-news, would you kindly cc: any
> responses directly to me please?
>
> -----------------------------------------------------------------------
> 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
>

-- 
John Wallace
University of Washington                    ^    ^    ^
Fisheries Research Institute               / \  / \  / \   ^
Box 357980                                 / \  / \   |   / \
Seattle, WA 98195-7980                      |    |  o__~  / \
PHONE   (206) 543-1513                  @ @         /\/\   |
FAX     (206) 685-7471                   ~    
E-MAIL        jw@u.washington.edu              o
WWW      http://www.fish.washington.edu/people/jrw/Wallace.html 
                                                o  _///_ //
                                                <`)=  _<<
                                                    \\\  \\

----------------------------------------------------------------------- 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

----------------------------------------------------------------------- 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