[R] Comparing and adding two data series

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 27 13:38:53 CEST 2009


Also:

rowsum(B, A)

On Thu, Aug 27, 2009 at 7:03 AM, Maithili Shiva<maithili_shiva at yahoo.com> wrote:
> Dear Gerrit Eichner
>
> Thanks a million. This only proves how powerful R is. I really appreciate your kind help.
>
> Sometimes I really wonder from where I can learn such commands.
>
> Thanks again.
>
> With warmest regards
>
> Maithili
>
> --- On Thu, 27/8/09, Gerrit Eichner <Gerrit.Eichner at math.uni-giessen.de> wrote:
>
>
> From: Gerrit Eichner <Gerrit.Eichner at math.uni-giessen.de>
> Subject: Re: [R] Comparing and adding two data series
> To: "Maithili Shiva" <maithili_shiva at yahoo.com>
> Date: Thursday, 27 August, 2009, 10:57 AM
>
>
> Try
>
> tapply( B, A, sum)
>
>
> On Thu, 27 Aug 2009, Maithili Shiva 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]]
>>
>>
>
> Best regards  --  Gerrit
> Best regards  --  Gerrit Eichner
> Viele Grüße  --  Gerrit
> Viele Grüße  --  Gerrit Eichner
> Viele Grüße  --  GE
> Grüße  --  Gerrit
> Grüße  --  Gerrit Eichner
> Grüße  --  GE
> Gruß  --  G
>
> ---------------------------------------------------------------------
> AOR Dr. Gerrit Eichner             Mathematical Institute, Room 305 E
> gerrit.eichner at math.uni-giessen.de   Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109              http://www.uni-giessen.de/~gcb7
> ---------------------------------------------------------------------
>
>
>      Love Cricket? Check out live scores, photos, video highlights and mor
>        [[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.
>
>




More information about the R-help mailing list