一个值在数据集中出现多少次的直方图

Histogram of how many times a value appears in a dataset

想象以下非常简单的数据集:

Variable
A
B
B
C
D
D
D
E
F
F
F

我想要得到的是这些值出现频率的直方图。如果您只是 运行 hist() 该数据,您将获得每个值的计数,但我真正想要的是:

+
+ +
+++
123

但是,您知道,不是 ASCII 艺术。主要是,它将数据集从可变特定频率折叠为 "Three values appeared once, one value appeared twice, and two values appeared three times."

看起来你想要一个茎叶图,可以用stem获得。

您可以使用 descr 包获得所需的结果。下面的代码

require(descr)
data("mtcars")
# Get the plot
freq(mtcars$gear, plot = TRUE)

会给你这张图表:

您可以 运行 table 列出所有数据,然后 hist 使用您想要的休息时间等:

hist(table(Variable))

使用对 table 的嵌套调用。这是一个使用来自 iris:

的变量的示例
> table(iris$Sepal.Width)    
  2 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9   3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9   4 4.1 4.2 4.4 
  1   3   4   3   8   5   9  14  10  26  11  13   6  12   6   4   3   6   2   1   1   1   1 
> table(table(iris$Sepal.Width))
 1  2  3  4  5  6  8  9 10 11 12 13 14 26 
 5  1  3  2  1  3  1  1  1  1  1  1  1  1

调用 table 一次会告诉您每个值出现的频率,然后调用 table 上的 table 会告诉您不同计数的频率。然后,您可以制作第二个 table 的 barplot 以可视化该结果:

barplot(table(table(iris$Sepal.Width)))

注意:一般来说,hist是用来汇总一个连续变量(以"bins"指定的数字或大小),而barplot是用来显示计数。