如何在R中对字符向量进行分组和计数

时间:2019-04-02 16:13:46

标签: r

我似乎无法按性别分组并计算随时间变化的偏好频率

尝试过:

    PreferenceOverTimeByAge <- group_by(all7Wide$a_sex_dv)%>%
                               count(all7Wide$'preference_over_time')

但出现错误:

  

UseMethod(“ group_by_”)中的错误:没有适用于   “ group_by_”应用于“字符”类的对象

表看起来像这样

all7Wide

pidp .  a_sex_dv .  preference_over_time . 
1 .     male .      Conservative_Labour_Conservative
2 .     female .    Labour_Labour_Labour
3 .     male .      Labour_Labour_Labour
4 .     male .      Labour_Labour_Labour
5 .     female .    Conservative_Labour_Labour

我想产生一个像这样的表


a_sex_dv  preference_over_time .               frequency
male .    Conservative_Labour_Conservative .    1
female .  Conservative_Labour_Conservative      0
male .    Labour_Labour_Labour                  2 
female .  Labour_Labour_Labour                  1
male .    Conservative_Labour_Labour .          0
female .  Conservative_Labour_Labour .          1

0 个答案:

没有答案