Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalized to 100 data within groups

I have a dataset that looks like this:

Var Trait   Value
0   Trait1  42.26
1   Trait1  41.81
2   Trait1  41.21
0   Trait2  47.82
1   Trait2  51.44
2   Trait2  51.42
0   Trait3  10.27
1   Trait3  10.63
2   Trait3  10.14

I would like to add a forth column with normalized to 100 data calculated like this for each trait and var1:

(42.26/42.26)*100=100

(41.81/42.26)*100=98.93

(41.21/42.26)*100=97.51

For trait 2 and var1=1, Value is larger than var1=0 Value so in this case row 5 will be calculated like this: (47.82/51.44)*100=92.94

like image 409
VasoGene Avatar asked Mar 15 '23 18:03

VasoGene


1 Answers

If your data is data.table:

data[, col4:= (Value/max(Value))*100, by= Trait]
like image 140
Soheil Avatar answered Mar 31 '23 08:03

Soheil