主要内容

Grouping Variables

什么是分组变量?

分组变量是用于组的实用程序变量,或分类,观察。分组变量对于按组进行总结或可视化数据有用。分组变量可以是任何这些数据类型:

  • Numeric vector

  • Logical vector

  • Character array

  • String array

  • 字符向量的单元格阵列

  • Categorical vector

A grouping variable must have the same number of observations (rows) as the table, dataset array, or numeric array you are grouping. Observations that have the same grouping variable value belong to the same group.

For example, the following variables comprise the same groups. Each grouping variable divides five observations into two groups. The first group contains the first and fourth observations. The other three observations are in the second group.

数据类型 Grouping Variable
Numeric vector [1 2 2 1 2]
Logical vector [0 1 1 0 1]
String array ["Male","Female","Female","Male","Female"]
字符向量的单元格阵列 {'Male','Female','Female','Male','Female'}
Categorical vector 男性女性男性女性

Use grouping variables with labels to give each group a meaningful name. A categorical vector is an efficient and flexible choice of grouping variable.

Group Definition

Typically, there are as many groups as unique values in the grouping variable. However, categorical vectors can have levels that are not represented in the data. The groups and the order of the groups depend on the data type of the grouping variable. SupposeG是一个分组变量。

  • IfGis a numeric or logical vector, then the groups correspond to the distinct values inG, in the sorted order of the unique values.

  • IfGis a character array, string array, or cell array of character vectors, then the groups correspond to the distinct elements inG,按照他们的第一个外观的顺序。

  • IfGis a categorical vector, then the groups correspond to the unique category levels inG,按顺序返回categories.

一些功能,如grpstats, accept multiple grouping variables specified as a cell array of grouping variables, for example,{G1,G2,G3}. In this case, the groups are defined by the unique combinations of values in the grouping variables. The order is decided first by the order of the first grouping variable, then by the order of the second grouping variable, and so on.

Analysis Using Grouping Variables

This table lists common tasks you might want to perform using grouping variables.

Grouping Task Function Accepting Grouping Variable
Draw side-by-side boxplots for data in different groups. boxplot
Draw a scatter plot with markers colored by group. g箭偶
Draw a scatter plot matrix with markers colored by group. gplotmatrix
Compute summary statistics by group. grpstats
测试组意味着之间的差异。 Anovan.
Create an index vector from a grouping variable. grp2idx

Missing Group Values

如果您包含有效指示符,则分组变量可能具有缺失的值。

Grouping Variable Data Type 缺少价值指标
Numeric vector NaN
Logical vector (Cannot be missing)
Character array Row of spaces
String array <缺失>要么""
字符向量的单元格阵列 ''
Categorical vector

See Also

Related Examples

More About