tabulate
频数表
说明
示例
为一个数据向量创建频数表。
加载 patients
数据集。显示 Gender
变量的前五个条目。每个值表示一个患者的性别。
load patients
Gender(1:5)
ans = 5×1 cell
{'Male' }
{'Male' }
{'Female'}
{'Female'}
{'Female'}
生成一个频数表,显示数据集中 Male
和 Female
患者的数量和百分比。
tabulate(Gender)
Value Count Percent Male 47 47.00% Female 53 53.00%
为一个正整数向量创建频数表。默认情况下,如果向量 x
只包含正整数,则 tabulate
将为 1
和 max(x)
之间没有出现在 x
中的整数返回 0 个计数。要避免此行为,请在调用 tabulate
之前将向量 x
转换为 categorical
向量。
加载 patients
数据集。显示 Height
变量的前五个条目。每个值表示一位患者的身高,以英寸为单位。
load patients
Height(1:5)
ans = 5×1
71
69
64
67
64
创建一个频数表,在其第二列和第三列显示数据集中具有特定身高的患者的数量和百分比。显示 tabulate
返回的矩阵的前五个条目和后五个条目。对于 1
和 72
英寸之间的每个高度,tbl
包含一个对应的行,其中 72
是 Height
中的最大高度值。
tbl = tabulate(Height); first = tbl(1:5,:)
first = 5×3
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
last = tbl(end-4:end,:)
last = 5×3
68 15 15
69 8 8
70 11 11
71 10 10
72 4 4
生成一个频数表,仅显示 Height
变量中出现的高度的 Count
和 Percent
值。将 Height
转换为一个 categorical
变量,然后调用 tabulate
函数。
newHeight = categorical(Height); tabulate(newHeight)
Value Count Percent 60 1 1.00% 62 3 3.00% 63 7 7.00% 64 12 12.00% 65 8 8.00% 66 15 15.00% 67 6 6.00% 68 15 15.00% 69 8 8.00% 70 11 11.00% 71 10 10.00% 72 4 4.00%
使用 tabulate
基于字符数组创建一个频数表。将生成的元胞数组转换为一个 table
数组,并可视化结果。
加载 carsmall
数据集。表格化 Origin
变量中的数据,显示数据集中每辆汽车的原产国。将生成的元胞数组 tbl
转换为一个 table
数组 t
。将 Value
列更改为一个 categorical
向量。
load carsmall tbl = tabulate(Origin); t = cell2table(tbl,'VariableNames', ... {'Value','Count','Percent'}); t.Value = categorical(t.Value)
t=6×3 table
Value Count Percent
_______ _____ _______
USA 69 69
France 4 4
Japan 15 15
Germany 9 9
Sweden 2 2
Italy 1 1
基于频数表创建一个条形图。
bar(t.Value,t.Count) xlabel('Country of Origin') ylabel('Number of Cars')
基于一个包含 NaN
值的数值向量创建一个频数表。
加载 carsmall
数据集。MPG
变量包含 100 辆汽车的每加仑英里数测量值。对于其中的六辆车,缺失 MPG
值 (NaN
)。
load carsmall
numcars = length(MPG)
numcars = 100
nanindex = isnan(MPG); numMissingMPG = length(MPG(nanindex))
numMissingMPG = 6
使用 MPG
创建一个频数表。将 tabulate
的矩阵输出转换为一个表,并为表列加标签。
tbl = tabulate(MPG); t = array2table(tbl,'VariableNames', ... {'Value','Count','Percent'})
t=37×3 table
Value Count Percent
_____ _____ _______
9 1 1.0638
10 2 2.1277
11 1 1.0638
13 4 4.2553
14 5 5.3191
14.5 1 1.0638
15 5 5.3191
15.5 1 1.0638
16 2 2.1277
16.5 2 2.1277
17 1 1.0638
17.5 2 2.1277
18 4 4.2553
18.5 1 1.0638
19 2 2.1277
20 2 2.1277
⋮
频数表仅显示具有数值 MPG
值的 94 辆汽车的数据。tabulate
计算此汽车子集(而不是 100 辆汽车的整个集合)中每个 MPG
值的百分比。
tnumcars = sum(t.Count)
tnumcars = 94
输入参数
替代功能
MATLAB® 包括函数
groupcounts
,该函数还返回组计数,建议您在处理表时使用。grpstats
函数还返回组计数和其他组汇总统计量。
扩展功能
tabulate
函数完全支持 tall 数组。有关详细信息,请参阅tall 数组。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2006a 之前推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)