Dear Peter Konings 
 
Thanks a lot for your kind advice. It worked wonderfully.
 
Thanks again
 
Regards
 
Maithili

--- On Thu, 27/8/09, Peter Konings <peter.l.e.konings@gmail.com> wrote:


From: Peter Konings <peter.l.e.konings@gmail.com>
Subject: Re: [R] Comparing and adding two data series
To: "Maithili Shiva" <maithili_shiva@yahoo.com>
Date: Thursday, 27 August, 2009, 11:12 AM


Hi Maithili, 

how about 

tapply(B, A, sum)

HTH
Peter.


On Thu, Aug 27, 2009 at 12:38 PM, Maithili Shiva <maithili_shiva@yahoo.com> wrote:

Dear R helpers
 
I have two series A and B as given below -
 
A <- c(2, 2, 1, 3, 7, 3, 3, 1, 14, 7, 31)

B <- c(0.0728,0.9538,4.0140,0.0020,2.5593,0.1620,2.513,0.3798, .0033,0.2282, 0.1614)
 
I need to calculate the total in dataset B corresponding to the numbers in dataset A i.e. for no 1 in A, I need the total as 4.0140+0.3798 (as 1 is repeated twice)
for no 2, I need the total as 0.0728+0.9538 (as 2 is also repeated tqice and so on)
Thus for no 31 in A, I should get only 0.1614.
 
 
I have written the R code but its not working. My code is as follows,
 
# --------------------------------------
 
D <- array()
 
i   = 1
for (i in 1:max(A))
{
D[i] = 0
i   = i + 1
}
 
# _____________________________________
 
T <- array()
 
k   = 1
m   = 1
 
for (k in 1:max(A))
{
 
for (m in 1:max(A))
{
 
 if (D[m]  == k)
 T[k]  = T[m] + B[m]
 
else
 
T[k] = T[m] + 0
 
m    = m + 1
}
 
k    = k + 1
}

# ---------------------------------------------------------------------
 
Please correct me. I think I have messed up with the loops but not able to understand where. Please guide me.
 
Thanking in advance
 
Maithili
 
 
 
 
 


     See the Web's breaking stories, chosen by people like you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/
       [[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.





      See the Web&#39;s breaking stories, chosen by people like you. Check 
	[[alternative HTML version deleted]]


