如何获得每个十分位数中一个因子变量的计数或百分比?

How to get the count or percentage of one factor variable in each decile ?

我的数据集就像下面的例子

Tier Decile
1     1
1     1
2     1
3     1
2     1
2     2
1     2
3     2
3     2
3     2
1     3
2     3
2     3
3     3
3     3

如果简单的计数或者可能是百分比,我想得到如下的答案。 lapply 或聚合函数可以工作吗?

Tier    Decile1  Decile2  Decile3
Tier=  1    2        1       1
Tier = 2    2        1       2
Tier = 3    1        2       2

使用table。假设 df 是您的 data.frame

> with(df, table(Tier, Decile))
    Decile
Tier 1 2 3
   1 2 1 1
   2 2 1 2
   3 1 3 2