efz
efz

Reputation: 435

pipes operation on the same column by groups

This is a recurrent issue for me. A part of my dataset:

   L2     L1    unc.max    unc.min    emi.agg
1 1.A    CH4 0.98020864 0.98010294   375913.3
2 1.A    CO2 0.06519989 0.06415013 28983572.2
3 1.A    N2O 8.10874574 2.38907240   289785.2
4 1.B    CH4 0.92234486 0.53458320  2942328.1
5 1.B    CO2 0.44141252 0.44093830   580784.1
6 1.A sumGHG 0.15541677 0.09848645 29649270.7
7 1.B sumGHG 0.84306330 0.51914587  3523112.2

I need for each group defined by L2 (thus 1.A and 1.B) to do some opearations between CH4,CO2,N2O elements and the corresponding sumGHG element. For example, I would like to create a new column so that the first row is the product of unc.max*emi.agg of that row divided by unc.max*emi.agg of the sumGHG row corresponding to the same L2. The first row would then be:

0.98020864*375913.3/0.15541677*29649270.7

the second would be:

0.06519989*28983572.2/0.15541677*29649270.7

and so forth. the rows corresponding to sumGHG will equal to one. I cannot figure out a pipes to work! The data:

structure(list(L2 = c("1.A", "1.A", "1.A", "1.B", "1.B", "1.A", 
"1.B"), L1 = c("CH4", "CO2", "N2O", "CH4", "CO2", "sumGHG", "sumGHG"
), unc.max = c(0.980208638698309, 0.0651998890654749, 8.1087457367104, 
0.922344859797637, 0.441412519786308, 0.155416769067452, 0.843063296024141
), unc.min = c(0.980102941492096, 0.0641501300870636, 2.38907239683858, 
0.534583203757245, 0.440938301221688, 0.0984864542571333, 0.519145867369634
), emi.agg = c(375913.308161253, 28983572.1535152, 289785.223284676, 
2942328.10140511, 580784.071175234, 29649270.6849611, 3523112.17258035
)), row.names = c(NA, -7L), class = "data.frame")

Upvotes: 1

Views: 49

Answers (1)

Maël
Maël

Reputation: 51974

I think something like this can do the trick, using group_by.

df %>% mutate(newcol = unc.max*emi.agg) %>% 
  group_by(L2) %>% mutate(newcol2 = newcol/newcol[L1=="sumGHG"])

I get :

# A tibble: 7 x 7
# Groups:   L2 [2]
  L2    L1     unc.max unc.min   emi.agg   newcol newcol2
  <chr> <chr>    <dbl>   <dbl>     <dbl>    <dbl>   <dbl>
1 1.A   CH4     0.980   0.980    375913.  368473.  0.0800
2 1.A   CO2     0.0652  0.0642 28983572. 1889726.  0.410 
3 1.A   N2O     8.11    2.39     289785. 2349795.  0.510 
4 1.B   CH4     0.922   0.535   2942328. 2713841.  0.914 
5 1.B   CO2     0.441   0.441    580784.  256365.  0.0863
6 1.A   sumGHG  0.155   0.0985 29649271. 4607994.  1     
7 1.B   sumGHG  0.843   0.519   3523112. 2970207.  1    

Upvotes: 1

Related Questions