I have the following table, which i would like to modify:
| type | position | ratio | number | percentage |
|---|---|---|---|---|
| DNA | intergenic | 0.00026933362 | 225173 | 40.757876065 |
| DNA | intragenic | 0.00021799943 | 41250 | 7.466536342 |
| LINE | intergenic | 0.00027633335 | 48619 | 8.800376494 |
| LINE | intragenic | 0.00031015097 | 9578 | 1.733684487 |
I want to add rows that contain the following modifications:
type: if the value "type" is identical between two rows (it always is in my case), add it again in a separate row of the column "type".
position: change the value from intergenic/intragenic to "genome" if (1)
ratio: ratio value would be the weighted mean calculated from the ratio of intergenic and intragenic rows of the same type value:
((number_intragenic * ratio_intragenic) + (number_intergenic * ratio_intergenic))/(number_intragenic + number_intergenic)number: sum of number values for the same type: sum(number_intergenic + number_intragenic)
sum of the percentage values for the same type: sum(percentage_intergenic + percentage_intragenic)
My problem is that I do not know how to add rows to dataframe by making specific calculation from already existing rows. It is easy to add columns using mutate in dplyr. How can I do this for rows?
I would much prefer if the solution is provided in dplyr.
Edit: The formula of the weighted mean was wrong. I had added a + sign instead of a * sign in the following part of the formula: (number_intergenic + ratio_intergenic). It has now been fixed.