Add column with results from a simple division

Hello,

I have a simple dataframe df :

lineage count
skin 82
kidney 19
bone 39
breast 11
peripheral_nervous_system 17
colorectal 36
ovary 14

Also, for each entry in the 'lineage' column, I have a separate numeric containing a value. So I have 7 numerics (skin is 47 for example).

How can I add a column called Result which is just this simple division of one of the numerics , divided by the respective value in count (referencing df) *100.

So the result for skin would be 47/82*100 = 57.13

lineage count Result
skin 82 57.13
kidney 19 xx.xx
bone 39 xx.xx
breast 11 xx.xx
peripheral_nervous_system 17 xx.xx
colorectal 36 xx.xx
ovary 14 xx.xx

Along the lines of

suppressPackageStartupMessages(library(dplyr))
mtcars[,10:11] -> a
a %>% mutate(carb = carb * 16) %>% mutate(result = carb/gear)
#>    gear carb    result
#> 1     4   64 16.000000
#> 2     4   64 16.000000
#> 3     4   16  4.000000
#> 4     3   16  5.333333
#> 5     3   32 10.666667
#> 6     3   16  5.333333
#> 7     3   64 21.333333
#> 8     4   32  8.000000
#> 9     4   32  8.000000
#> 10    4   64 16.000000
#> 11    4   64 16.000000
#> 12    3   48 16.000000
#> 13    3   48 16.000000
#> 14    3   48 16.000000
#> 15    3   64 21.333333
#> 16    3   64 21.333333
#> 17    3   64 21.333333
#> 18    4   16  4.000000
#> 19    4   32  8.000000
#> 20    4   16  4.000000
#> 21    3   16  5.333333
#> 22    3   32 10.666667
#> 23    3   32 10.666667
#> 24    3   64 21.333333
#> 25    3   32 10.666667
#> 26    4   16  4.000000
#> 27    5   32  6.400000
#> 28    5   32  6.400000
#> 29    5   64 12.800000
#> 30    5   96 19.200000
#> 31    5  128 25.600000
#> 32    4   32  8.000000

Created on 2020-08-03 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.