[R] reshaping result of by()
Sebastian Luque
spluque at gmail.com
Sat Feb 18 23:07:42 CET 2006
Hello,
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!
Cheers,
--
Sebastian P. Luque
More information about the R-help
mailing list