Include zero frequencies in frequency table for Likert data
frequency, r
Solution
`table` produces a contingency table, while `tabular` produces a frequency table that includes zero counts.
tabulate(data)
# [1] 3 1 0 2 1
Another way (if you have integers starting from 1 - but easily modifiable for other cases):
setNames(tabulate(data), 1:max(data)) # to make the output easier to read
# 1 2 3 4 5
# 3 1 0 2 1
Problem
I have a dataset with responses to a Likert item on a 9pt scale. I would like to create a frequency table (and barplot) of the data but some values on the scale never occur in my dataset, so `table()` removes that value from the frequency table. I would like it instead to present the value with a frequency of `0`. That is, given the following dataset ``` # Assume a 5pt Likert scale for ease of example data <- c(1, 1, 2, 1, 4, 4, 5) ``` I would like to get the following frequency table without having to manually insert a column named `3` with the value `0`. ``` 1 2 3 4 5 3 1 0 2 1 ``` I'm new to `R`, so maybe I've overlooked something basic, but I haven't come across a function or option that gives the desired result.