在频率表中包括零频率以获取李克特数据 [英] Include zero frequencies in frequency table for Likert data
问题描述
我有一个数据集,该数据集以9pt的比例对李克特项目做出了回应.我想创建数据的频率表(和条形图),但是刻度中的某些值从未出现在我的数据集中,因此table()
从频率表中删除该值.我希望它以0
的频率显示值.也就是说,给定以下数据集
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)
我想获得以下频率表,而不必手动插入值为0
的名为3
的列.
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
我是R
的新手,所以也许我忽略了一些基本知识,但是我没有遇到能够提供所需结果的函数或选项.
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.
推荐答案
table
生成列联表,而tabular
生成包含零计数的频率表.
table
produces a contingency table, while tabular
produces a frequency table that includes zero counts.
tabulate(data)
# [1] 3 1 0 2 1
另一种方式(如果您具有从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
这篇关于在频率表中包括零频率以获取李克特数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!