如何使用data.table获取R中键的每个值的百分比? [英] How to obtain percentages per value for the keys in R using data.table?

查看:64
本文介绍了如何使用data.table获取R中键的每个值的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 data.table 如下所示:

x, y, sum(count)
1, 1, 3
1, 2, 7
1, 3, 8
2, 1, 4
2, 2, 3
2, 3, 10

依此类推。它以 x y 为键,我做了一个 sum 计数列上。我想按x的值将其分解为百分比,这样它就变成:

And so on. It's keyed by x and y and I did a sum on a count column. I would like to break it down into percentages by the values of x so that it becomes:

x, y, percentage(counts)
1, 1, 16.7
1, 2, 38.9
1, 3, 44.4
2, 1, 23.5
2, 2, 17.6
2, 3, 58.8

因此,每个 x 值总计为100%。我正在使用 data.table 包进行此操作。预先感谢您的帮助。

So that the total percentage per x value totals 100%. I am doing this using the data.table package. Thank you in advance for your help.

推荐答案

我不太了解 data.table 解决方案已经发布,所以我会这样做(并且我会更改列的名称以不使用括号,以避免对列名称使用大量反引号(!))

I don't quite understand the data.table solution already posted, so I would do it like this (and I would change the name of the columns to not have parentheses to avoid lots of backtick quoting(!) of column names):

dt[ , `percentage(counts)` := `sum(count)` / sum( `sum(count)` ) * 100 , by = "x" ]
#   x y sum(count) percentage(counts)
#1: 1 1          3           16.66667
#2: 1 2          7           38.88889
#3: 1 3          8           44.44444
#4: 2 1          4           23.52941
#5: 2 2          3           17.64706
#6: 2 3         10           58.82353

这篇关于如何使用data.table获取R中键的每个值的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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