Change the data.table column based on another column and add a new column

I have data.table DTwith two columns

   V1 V2
1:  1  3
2:  2  4
3:  3  5
4:  2  2
5:  3  8
6:  1  4
7:  2  5

For each row, I want to take all the records with the same V1and add records V2, then divide the record V2by this amount and add a third column. For example, in line 1, column 3, the answer should be 3 / (3 + 4). In line 2, column 3, the answer should be 4 / (4 + 2 + 5), etc.

I ultimately had to

   V1 V2 V3
1:  1  3 0.4285714
2:  2  4 0.3636364
3:  3  5 0.3846154
4:  2  2 0.1818182
5:  3  8 0.6153846
6:  1  4 0.5714286
7:  2  5 0.4545455

I can get V3through q <- DT[,V2/sum(V2),by='V1'], but then the strings are in the wrong order

   V1        V1
1:  1 0.4285714
2:  1 0.5714286
3:  2 0.3636364
4:  2 0.1818182
5:  2 0.4545455
6:  3 0.3846154
7:  3 0.6153846

so just pasting the second column qin DTwill not work. In addition, several uncomfortable data.table qnow has two columns with the same name V1.

, . .

+4
1

,

DT[,list(V2=V2, V3=V2/sum(V2)), by='V1']

( ) data.table :

DT[, V3 := V2/sum(V2), by='V1'] 

, .

, RTFM ( , data.table, , 30 !)

, , . , , , , .

+4

Source: https://habr.com/ru/post/1534459/


All Articles