我通常在Stata中使用命令proportion var1, over(var2)
来获取由var2分层的var1的比例。
我想知道如何在R
中做到这一点?
Stata输出:
. prop CS_RACA2, over(SEM_PRI_CAT2 )
Proportion estimation Number of obs = 6,613
Branca: CS_RACA2 = Branca
Preta: CS_RACA2 = Preta
Parda: CS_RACA2 = Parda
Ignorado: CS_RACA2 = Ignorado
_prop_5: CS_RACA2 = Amarela/Indigena
_subpop_1: SEM_PRI_CAT2 = 1 a 16
_subpop_2: SEM_PRI_CAT2 = 17 a 21
_subpop_3: SEM_PRI_CAT2 = 22 a 25
_subpop_4: SEM_PRI_CAT2 = 26 a 29
--------------------------------------------------------------
Over | Proportion Std. Err. [95% Conf. Interval]
-------------+------------------------------------------------
Branca |
_subpop_1 | .3179625 .0107862 .2972029 .3394717
_subpop_2 | .3071672 .0120568 .2840509 .3312943
_subpop_3 | .2307692 .0095756 .2125342 .250072
_subpop_4 | .2340267 .0115446 .212159 .2574121
-------------+------------------------------------------------
Preta |
_subpop_1 | .0825737 .006375 .0709064 .0959625
_subpop_2 | .0798635 .0070848 .067032 .0949014
_subpop_3 | .1104801 .0071247 .0972671 .1252391
_subpop_4 | .0676077 .006846 .055361 .0823276
-------------+------------------------------------------------
Parda |
_subpop_1 | .4541555 .0115322 .4316582 .4768415
_subpop_2 | .4627986 .0130315 .4373723 .48842
_subpop_3 | .4889004 .0113608 .4666662 .5111786
_subpop_4 | .4940565 .0136326 .4673746 .5207723
-------------+------------------------------------------------
Ignorado |
_subpop_1 | .1383378 .0079968 .1233938 .1547721
_subpop_2 | .1392491 .0090482 .1224397 .1579509
_subpop_3 | .1641714 .0084189 .1483267 .1813482
_subpop_4 | .1953938 .0108115 .1750653 .2174605
-------------+------------------------------------------------
_prop_5 |
_subpop_1 | .0069705 .001927 .0040507 .0119698
_subpop_2 | .0109215 .0027164 .0066996 .0177564
_subpop_3 | .0056789 .0017078 .0031468 .0102274
_subpop_4 | .0089153 .0025631 .0050683 .0156366
--------------------------------------------------------------
答案 0 :(得分:2)
我认为这就是您要寻找的东西
library(dplyr)
df %>%
group_by(var2) %>%
mutate(prop = var1 / sum(var2))
编辑:%>%
已插入