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

Z. Todd Taylor (Todd.Taylor@pnl.gov)
Fri, 24 Apr 1998 14:02:05 -0700

Frank E Harrell Jr <fharrell@virginia.edu> wrote:

> What is the most efficient method for creating a data frame containing vectors of
> summary statistics? For example, suppose that data frame d contains age.group, sex, and y.
> I want to make a new data frame containing the combinations of age.group and sex
> that occurred in the data, along with a new matrix variable or two new variables containing
> the 0.25 and 0.75 quantiles. Then I'll pass the new summary data frame to trellis for
> graphing results. tapply will do the calculations I need but the resulting format is a
> matrix rather than a data frame containing combinations of stratification variables.

If I understand the question, I think you just need to convert
the matrix to an equivalent data frame. I use my function
'dfify' (which is short for data-frame-ify). It's a wrapper
around expand.grid() that converts a matrix (or multi-way array)
to an equivalent and nicely named data frame:

"dfify" <- function(arr, value.name="value", dn.names=names(dimnames(arr))) {

dn <- dimnames(arr <- as.array(arr))
if ( is.null(dn) ) stop("Can't data-frame-ify an array without dimnames")
names(dn) <- dn.names

ans <- cbind(expand.grid(dn), as.vector(arr))
names(ans)[ncol(ans)] <- value.name
ans
}

e.g.,

x <- cbind(1:3, 4:6)
dimnames(x) <- list(animal=c("dog", "cat", "pig"),
sex=c("male", "female")
)
print(x)

male female
dog 1 4
cat 2 5
pig 3 6

print(dfify(x, value.name="number"))

animal sex number
1 dog male 1
2 cat male 2
3 pig male 3
4 dog female 4
5 cat female 5
6 pig female 6

--Todd

```--
Z. Todd Taylor
Pacific Northwest National Laboratory
Todd.Taylor@pnl.gov
Why do you drive on a parkway and park on a driveway?
-----------------------------------------------------------------------
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
```