将没有出现的值添加到 R 中的 table

Add value with no occurrences to table in R

假设我在数据框列中有一系列考试成绩:

grades <- c("B", "C", "C", "C", "D", "D", "E", "F", "F")
grades.df <- data.frame(grades)

在说明这一点时,插图会有点误导,因为它没有显示每个人都希望达到的成绩的 0:"A":

barplot(table(grades))

如何向此 table 添加出现次数为 0 的 "A",使其出现在高度为零的条形图中?

使用具有适当级别的 factor

grades <- factor(c("B", "C", "C", "C", "D", "D", "E", "F", "F"),levels=LETTERS[1:6])
table(grades)
grades
A B C D E F 
0 1 3 2 1 2 

barplot(table(grades))