计算 R 数据框中特定字符串的数量

use*_*499 1 string r unique count dataframe

我有一个包含很多列的数据框,但我感兴趣的两列是专业和部门。我需要找到一种方法来计算列中特定条目的数量。所以我的数据框看起来像

student_num    major          dept
123            child          education
124            child          education
125            special        education
126            justice        administration
127            justice        administration
128            justice        administration
129            police         administration
130            police         administration
Run Code Online (Sandbox Code Playgroud)

我想要的是每个专业和部门的学生人数。就像是

education   child   special    administration    justice    police
3           2       1          5                 3          2
Run Code Online (Sandbox Code Playgroud)

我尝试了几种方法,但没有什么是我需要的。我尝试使用来自plyr的aggregate()函数和ddply(),但他们给了我两个部门——两个独特的条目,教育和管理。我如何计算每个唯一条目而不是有多少个唯一条目?

小智 6

你可以试试:

library(dplyr)
count(my_dataframe, major)
count(my_dataframe, dept)
Run Code Online (Sandbox Code Playgroud)