使用 collection 上的序列的 R 频率表

R frequency tables using a sequence over a collection

我有这个collection

x <- c(3,4,5,7,7,9,9,9,10,10,10,10,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,15,15)

我想获得 3:15 序列中每个值在 collection 中的频率。如果我这样做 table(x) 它会给我现有值的频率,但是例如,值 6 的频率值为 0 并且不会显示 table().

table 中使用 factorlevels

table(factor(x, levels = 3:15))

# 3  4  5  6  7  8  9 10 11 12 13 14 15 
# 1  1  1  0  2  0  3  4  7 10 14  3  2 

或者对于一般情况:

table(factor(x, levels = min(x):max(x)))