本文介绍了如何对不同的列进行分组并根据一列找到百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按 var1
和 var2
对以下数据表进行分组,然后在<$ c中找到百分比$ c> var2 :
I want to group the following data table by var1
and var2
and then find the percentage in var2
:
data <- as.data.table(list(var1 = c("x1","x1","x2","x1","x2"),
var2 = c("y1","y1","y1","y2","y2")))
data[, .(count = .N), by=.(var1, var2)]
# var1 var2 count
#1: x1 y1 2
#2: x2 y1 1
#3: x1 y2 1
#4: x2 y2 1
这是我感兴趣的结果:
# var1 var2 count ratio in var2
#1: x1 y1 2 0.66
#2: x2 y1 1 0.33
#3: x1 y2 1 0.5
#4: x2 y2 1 0.5
如何更改代码以实现此目的?
How to change the code to achieve this?
推荐答案
这应该给您您想要的东西:
This should give you what you want:
data <- data[, .N, by = .(var1, var2)][, ratio:=N/sum(N), by = var2]
这将导致:
> data
var1 var2 N ratio
1: x1 y1 2 0.6666667
2: x2 y1 1 0.3333333
3: x1 y2 1 0.5000000
4: x2 y2 1 0.5000000
这篇关于如何对不同的列进行分组并根据一列找到百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!