Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include zero frequencies in frequency table for Likert data

Tags:

r

frequency

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.

like image 560
ThomasH Avatar asked Dec 04 '12 14:12

ThomasH


People also ask

Why should I learn frequency distribution tables in R?

If you plan on entering the industry as a data science analyst or even if your work remotely involves the use of data manipulation to make decisions, then you will be coming across frequency distribution tables all the time. It is therefore essential to have an understanding of how relative frequency tables work and how to make a table in R.

How many variables can I list in a frequency table?

(Note that SAS will recognize both TABLE and TABLES .) You can list as many variables as you want, with each variable separated by a space. If the TABLES statement is not included, then SAS will generate a table for every variable in the dataset. This is all that is required to produce basic frequency tables,...

What is the frequency of a data set?

Frequency is the number of times a specific data value occurs in your dataset. A frequency table lists a set of values and how often each one appears. They help you understand which data values are common and which are rare.

What is a relative frequency table in R?

Relative Frequency Table in R Cross Tabulation Using ‘gmodels’ Up till now, we have talked about frequency (or the count of appearance) of one variable in a data set, but for data analysts, an important task would be to generate a frequency with 2, 3 or even more variables. Such a table is also called a Cross Table or a Contingency Table.


2 Answers

EDIT:

tabular produces frequency tables while table 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 counted

data <- factor(data, levels = c(1:5))
table(data)

produces the desired output

like image 61
ThomasH Avatar answered Nov 03 '22 02:11

ThomasH


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 
like image 31
lebatsnok Avatar answered Nov 03 '22 02:11

lebatsnok