tabulate()

  • (Exercise 5-15)
y <- c("a", "b", "a", "c", "a", "d", "e", "e", "g", "g", "c", "c", "g")
result <- structure(tabulate(factor(y)), # y must be a factor or numeric
                    names = letters[match(unique(y), letters)])
print(result)
## a b c d e g 
## 3 1 3 1 2 3