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.
EDIT:
tabular
produces frequency tables whiletable
produces contingency tables. However, to get zero frequencies in a one-dimensional contingency table as in the above example, the below code still works, of course.This question provided the missing link. By converting the Likert item to a factor, and explicitly specifying the levels, levels with a frequency of
0
are still countedproduces the desired output
If you want to quickly calculate the counts or proportions for multiple likert items and get your output in a data.frame, you may like the function
psych::response.frequencies
in thepsych
package.Lets create some data (note that there are no 9s):
If you want to calculate the proportion in each category
you get the following:
If you want counts, you can multiply by the sample size:
You get the following:
A few notes:
max
is 10. Thus, if you have more than 10 response options, you'll have issues. Otherwise, in your case, and many Likert item cases, you could omit themax
argument.uniqueitems
specifies the possible values. If all your values were present in at least one item, then this would be inferred from the data.table
produces a contingency table, whiletabular
produces a frequency table that includes zero counts.Another way (if you have integers starting from 1 - but easily modifiable for other cases):