Is this what you want?
> x <- lapply(split(seq(nrow(toydf)), list(toydf[[2]], toydf[[3]])),
function(.indx){
+ .cnt <- getFreq(toydf[.indx,1], bks=0:50)
+ cbind(toydf[.indx, 2:3], .cnt)
+ })> do.call('rbind', x)
Var2 Var3 1 2
A.pop1.1 A pop1 1 2
A.pop1.2 A pop1 2 0
A.pop1.3 A pop1 3 1
A.pop1.4 A pop1 4 0
A.pop1.5 A pop1 5 1
A.pop1.6 A pop1 6 1
A.pop1.7 A pop1 7 0
A.pop1.8 A pop1 8 0
A.pop1.9 A pop1 9 2
A.pop1.10 A pop1 10 3
A.pop1.11 A pop1 11 0
A.pop1.12 A pop1 12 0
A.pop1.13 A pop1 13 2
A.pop1.14 A pop1 14 0
............
On 2/18/06, Sebastian Luque <spluque@gmail.com>
wrote:>
> 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What the problem you are trying to solve?
[[alternative HTML version deleted]]