[R] reshaping result of by()

From: Sebastian Luque <spluque_at_gmail.com>
Date: Sun 19 Feb 2006 - 09:07:42 EST


I'm trying to build a frequency table for a vector, broken down by the combination of factors. For further analyses, I need to have the result arranged in a new data frame, with the upper limit of the histogram's breaks, the per bin count, and the factors identifying each record. My problem is including the latter:

---<---------------cut here---------------start-------------->---

toydf <- expand.grid(sample(0:50, 50, TRUE), c("A", "B"),

                     c("pop1", "pop2", "pop3", "pop4", "pop5"))

"getFreq" <- function(x, bks)
  fhist <- hist(x, breaks = bks, plot = FALSE,

                include.lowest = TRUE)
  matrix(c(fhist$breaks[-1], fhist$counts),
           nrow = length(fhist$breaks[-1]), ncol = 2)

freqs <- by(toydf[[1]], list(toydf[[2]], toydf[[3]]), getFreq, bks = 0:50)

---<---------------cut here---------------end---------------->---

so I'd need to do some manipulation with dimnames(freqs), which contains the levels of the factors supplied to 'getFreq', to assign the factors to each element of the matrix value of 'by'. I could then do:

do.call(rbind, freqs)

to obtain the final data frame, which would then have all the necessary information.

Any help with manipulation of the dimnames(freqs) (or something even better) would be very appreciated!


Sebastian P. Luque

R-help@stat.math.ethz.ch mailing list
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sun Feb 19 09:31:40 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:35 EST