Hi, I have a dataset with three column like this x1 x2 Count 1 0 523 1 0 23 0 0 2 0 1 45 0 0 3 1 1 433 I need to create a loop so that when c(x1,x2)=c(1,1), I can add the corresponding Counts.When c(x1,x2)=c(1,0), can add the corresponding counts and so on. Can anyone help me???? [[alternative HTML version deleted]]
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) t.sum(t, 1, 0) # [1] 546 t.sum(t, 0, 0) # [1] 5 Hope this helps a little. Allan On 03/06/10 16:18, Geeti Khan wrote:> Hi, > I have a dataset with three column like this > x1 x2 Count > 1 0 523 > 1 0 23 > 0 0 2 > 0 1 45 > 0 0 3 > 1 1 433 > > I need to create a loop so that when c(x1,x2)=c(1,1), I can add the corresponding Counts.When c(x1,x2)=c(1,0), can add the corresponding counts and so on. Can anyone help me???? > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hi Geeti, If d is your data.frame, the following is an option: as.data.frame.table(with(d, tapply(Count, list(x1, x2), sum))) HTH, Jorge On Thu, Jun 3, 2010 at 11:18 AM, Geeti Khan <> wrote:> Hi, > I have a dataset with three column like this > x1 x2 Count > 1 0 523 > 1 0 23 > 0 0 2 > 0 1 45 > 0 0 3 > 1 1 433 > > I need to create a loop so that when c(x1,x2)=c(1,1), I can add the > corresponding Counts.When c(x1,x2)=c(1,0), can add the corresponding counts > and so on. Can anyone help me???? > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 03.06.2010 18:18:33:> 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) > t.sum(t, 1, 0) > # [1] 546 > t.sum(t, 0, 0) > # [1] 5If 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 Regards Petr> > Hope this helps a little. > > Allan > > On 03/06/10 16:18, Geeti Khan wrote: > > Hi, > > I have a dataset with three column like this > > x1 x2 Count > > 1 0 523 > > 1 0 23 > > 0 0 2 > > 0 1 45 > > 0 0 3 > > 1 1 433 > > > > I need to create a loop so that when c(x1,x2)=c(1,1), I can add the > corresponding Counts.When c(x1,x2)=c(1,0), can add the correspondingcounts> and so on. Can anyone help me???? > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
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