Hi to all is there any construct to sum data=data.frame(row1=c(1,1,3,1,2,3,2,2,1,3,4,5,2,3,2,1) , row2=c(2,2,1,1,1,2,1,2,1,1,1,1,2,2,2,1) ) Means I would like to get all y of row1 if in row2 of the data.frame is an x f.e row1=3 and row2=2 so I would like to get 6 And is there another construct to get the count of pairs where row1=3 and row2=2 means the result should be 2 I could do this with loop but I am wondering whether there is any better way. Regards Knut
Try this: sum(data[with(data, row1 == 3 & row2 == 2),1]) and sum(with(data, row1 == 3 & row2 == 2)) On Fri, Nov 13, 2009 at 4:59 PM, Knut Krueger <rh at krueger-family.de> wrote:> Hi to all > is there any construct to sum > data=data.frame(row1=c(1,1,3,1,2,3,2,2,1,3,4,5,2,3,2,1) , > ? ? ? ? ? ? ? ? ? ? ? ? row2=c(2,2,1,1,1,2,1,2,1,1,1,1,2,2,2,1) ) > Means I would like to get all y of row1 if in row2 of the data.frame is an x > f.e row1=3 and row2=2 > so I would like to get 6 > > And is there another construct to get the count of pairs where > row1=3 and row2=2 > means the result should be 2 > I could do this with loop but I am wondering whether there is any better > way. > > Regards Knut > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi Knut, Try sum(subset(df, row1 == 3 & row2 == 2)[,1]) and nrow(subset( df, row1 == 3 & row2 == 2) ) HTH, Jorge On Fri, Nov 13, 2009 at 1:59 PM, Knut Krueger <> wrote:> Hi to all > is there any construct to sum > data=data.frame(row1=c(1,1,3,1,2,3,2,2,1,3,4,5,2,3,2,1) , > row2=c(2,2,1,1,1,2,1,2,1,1,1,1,2,2,2,1) ) > Means I would like to get all y of row1 if in row2 of the data.frame is an > x > f.e row1=3 and row2=2 > so I would like to get 6 > > And is there another construct to get the count of pairs where > row1=3 and row2=2 > means the result should be 2 > I could do this with loop but I am wondering whether there is any better > way. > > Regards Knut > > ______________________________________________ > 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]]