Re: [R] For Loop help needed

From: Allan Engelhardt <allane_at_cybaea.com>
Date: Fri, 04 Jun 2010 11:01:49 +0100

On 04/06/10 10:32, Petr PIKAL wrote:
>> One option:
>>
>> t<- data.frame(x1=c(1,1,0,0,0,1), x2=c(0,0,0,1,0,1),
>> Count=c(523,23,2,45,3,433))
>> t.sum<- function(df, x1, x2) sum(df[df$x1==x1& df$x2==x2,]$Count)
>> [...]
> If this is what Khan wants so
>
> aggregate(t$Count, list(interaction(t$x1, t$x2)), sum)
> Group.1 x
> 1 0.0 5
> 2 1.0 546
> 3 0.1 45
> 4 1.1 433
>
> could be better option
>

Indeed it is better! Or even shorter with the formula interface:

aggregate(Count ~ x1+x2, data=t, sum)

#   x1 x2 Count
# 1  0  0     5
# 2  1  0   546
# 3  0  1    45
# 4  1  1   433


Allan



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 04 Jun 2010 - 10:05:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Jun 2010 - 10:40:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive