Hi, How can I accomplish this in R. Example: R1 R2 3 101 4 102 3 102 18 102 11 101 I want to find Sum(101) = 14 - i.e SUM(R1) where R2 = 101 Sum(102) = 25 - SUM(R2) where R2 = 102 TIA Sachin --------------------------------- [[alternative HTML version deleted]]
Using the built in data.frame iris sum each of the first 4 columns for each value of the 5th column. rowsum(iris[,-5], iris[,5]) On 4/20/06, Sachin J <sachinj.2006 at yahoo.com> wrote:> Hi, > > How can I accomplish this in R. Example: > > R1 R2 > 3 101 > 4 102 > 3 102 > 18 102 > 11 101 > > I want to find Sum(101) = 14 - i.e SUM(R1) where R2 = 101 > Sum(102) = 25 - SUM(R2) where R2 = 102 > > TIA > Sachin > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 >
On Thu, 2006-04-20 at 11:46 -0700, Sachin J wrote:> Hi, > > How can I accomplish this in R. Example: > > R1 R2 > 3 101 > 4 102 > 3 102 > 18 102 > 11 101 > > I want to find Sum(101) = 14 - i.e SUM(R1) where R2 = 101 > Sum(102) = 25 - SUM(R2) where R2 = 102 > > TIA > SachinPresuming that your data is in a data frame called DF:> DFR1 R2 1 3 101 2 4 102 3 3 102 4 18 102 5 11 101 At least three options:> with(DF, tapply(R1, R2, sum))101 102 14 25> aggregate(DF$R1, list(R2 = DF$R2), sum)R2 x 1 101 14 2 102 25> by(DF$R1, DF$R2, sum)INDICES: 101 [1] 14 ------------------------------------------------------ INDICES: 102 [1] 25 See ?by, ?aggregate and ?tapply and ?with. HTH, Marc Schwartz