Lists, Matrices, and Tables
Tables
Counting Categories
table counts how many times each category appears. Names on the result identify the categories.
Program
Play the script to count colors and pick the most frequent one.
tables.R
colors <- c("red", "blue", "red", "green", "red")
counts <- table(colors)
top <- names(counts)[which.max(counts)]
cat(top, "\n", sep = "")
table
`table(colors)` counts each distinct value.
names
`names(counts)` exposes category labels.
which.max
`which.max(counts)` finds the position of the largest count.