Does this do what you want?

> x
        date type count value
1 2006-04-01    A    10  99.6
2 2006-04-01    B     4  33.2
3 2006-04-02    A    22  43.2
4 2006-04-02    B     8  44.9
5 2006-04-03    A    12  12.4
6 2006-04-03    B    14  18.5
> y <- lapply(split(1:nrow(x), x$type), function(.ind){
+     c(count=sum(x$count[.ind]), value=mean(x$value[.ind]))
+ })
> do.call('rbind', y)
  count    value
A    44 51.73333
B    26 32.20000
>



On 4/28/06, kavaumail-r@yahoo.com <kavaumail-r@yahoo.com> wrote:
>
> I would like to use aggregate() to combine statistics
> for several days in a data frame. My data frame looks
> similar to this:
>
>   date        type  count  value
> 1  2006-04-01     A     10   99.6
> 2  2006-04-01     B      4   33.2
> 3  2006-04-02     A     22   43.2
> 4  2006-04-02     B      8   44.9
> 5  2006-04-03     A     12   12.4
> 6  2006-04-03     B     14   18.5
>
> ('date' is a factor, and my actual data frame has
> about 100 different 'types', not just two)
>
> I would like to sum up the 'counts' per 'type', and
> get an average of the 'values' per 'type'. In other
> words, I would like my results to look like this:
>
>   type  count  value
> 1  A     44     51.73333
> 2  B     26     32.2
>
> The way I'm doing this now is to tear the table apart
> into its individual columns, then apply aggregate() to
> each column individually (using the 'type' column for
> the 'by' parameter), and finally putting everything
> back together, like this:
>
> > A.count = aggregate(A$count, list(type=A$type), sum)
> > A.value = aggregate(A$value, list(type=A$type),
> mean)
> > B = data.frame(type=A.count$type, count=A.count$x,
> value=A.value$x)
>
> My actual table is a bit more involved than in this
> simple example, however, so this becomes quite
> tedious.
>
> I am hoping that there is a simpler way for doing
> this, for example by providing different FUN
> parameters for each column to the aggregate()
> function.
>
> I would appreciate any suggestions.
> Thanks
> Klaus
>
> ______________________________________________
> 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 (Cell)
+1 513 247 0281 (Home)

What the problem you are trying to solve?

	[[alternative HTML version deleted]]


