基于另一列修改数据表的列,并添加新列 [英] Modify column of a data.table based on another column and add the new column

查看:104
本文介绍了基于另一列修改数据表的列,并添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 data.table DT ,有两列

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

对于每一行,我想使用相同的 V1 获取所有条目并添加 V2 条目,然后将 V2 条目,并在第三列中添加。例如,在第1行第3列中,答案应该是3 /(3 + 4)。在第2行第3列答案应该是4 /(4 + 2 + 5)等。

For each row I want to take all the entries with the same V1 and add the V2 entries then divide the V2 entry by that sum and add in a third column. For example, in row 1, column 3 the answer should be 3/(3+4). In row 2, column 3 the answer should be 4/(4+2+5), etc.

最终我应该有

   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



p可以通过 q <-DT [,V2 / sum(V2),通过= 获得 V3 'V1'] ,但是行的顺序错误

I can get V3 via q <- DT[,V2/sum(V2),by='V1'] but then the rows 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

因此只需粘贴 q DT 将不起作用。此外,有些尴尬地, data.table q 现在有两个相同名称的列, V1

so simply pasting the second column of q to DT will not work. Also, somewhat awkwardly the data.table q now has two columns with the same name, V1.

我一直在关注这个问题几天,搜索高低,仍然不能想出一个简单的答案。

I have been banging my head on this problem for a few days now, searched high and low and still cannot come up with a simple answer. Any help would be much appreciated.

推荐答案

使用

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

(修改后的行顺序)或修改 data.table

(with modified row order) or modify the data.table in place using the assignment operator:

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

请注意,现在的行顺序是一样的。

Note that now the row order is the same.

RTFM (我要问的问题的一半 data.table 如果我花了额外的30分钟时间,我可以回答自己 it !)

Please RTFM (half the questions I ask about data.table I could have answered myself if I spent an extra 30 minutes perusing it!)

对于新表中的行顺序,I不要以为你可以轻易保存它。
还不清楚为什么要保留此订单,除非有另一个列已经指定它,在这种情况下,您可以相应地对新表进行排序。

As for the row order in the new table, I don't think you can easily preserve it. It is also unclear why you would want to preserve the order unless there is another column which dictates it already, in which case you can sort the new table accordingly.

这篇关于基于另一列修改数据表的列,并添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆