Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

findgroups

查找组并返回组编号

说明

要将数据分成若干组并将函数应用于这些组,请同时使用 findgroupssplitapply 函数。有关数据组计算的详细信息,请参阅对数据组的计算

示例

G = findgroups(A) 返回 G,它是从分组变量 A 生成的组编号的向量。输出参量 G 包含从 1 到 N 的整数值,指示 AN 个唯一值的 N 个不同组。例如,如果 A["b","a","a","b"],则 findgroups 返回 G[2 1 1 2]

要使用 G 基于其他变量拆分数据组,请将其作为 splitapply 函数的输入参量传入。

findgroups 函数将 A 中的空字符向量和 NaNNaT 以及未定义的分类值视为缺失值,并返回 NaN 作为 G 的对应元素。

示例

G = findgroups(A1,...,AN) 根据 A1,...,AN 生成组编号。findgroups 函数将组定义为 A1,...,AN 中唯一的值组合。例如,如果 A1["a","a","b","b"]A2[0 1 0 0],则 findgroups(A1,A2) 返回 G[1 2 3 3],因为组合 "b" 0 出现两次。

示例

[G,ID] = findgroups(A) 还会在 ID 中返回每个组的唯一值。例如,如果 A["b","a","a","b"],则 findgroups 返回 G[2 1 1 2],并返回 ID["a","b"]。参量 AID 具有相同的数据类型,但不需要具有相同的大小。

示例

[G,ID1,...,IDN] = findgroups(A1,...,AN) 还返回 ID1,...,IDN 中每个组的唯一值。ID1,...,IDN 中的值定义各组。例如,如果 A1["a","a","b","b"]A2[0 1 0 0],则 findgroups(A1,A2) 返回 G[1 2 3 3],并返回 ID1ID2["a","a","b"][0 1 0]

示例

G = findgroups(T) 返回 G,它是从表 T 中的变量生成的组编号的向量。findgroups 函数将 T 中的所有变量视为分组变量。

示例

[G,TID] = findgroups(T) 还返回一个表 TID,其中包含每个组的唯一值。TID 包含 T 的所有变量中唯一的值组合。TTID 中的变量具有相同的名称,但这些表不需要具有相同的行数。

示例

全部折叠

使用组编号将患者体重测量值分为吸烟者和非吸烟者两个体重组。然后,计算每组患者的平均体重。

从示例文件 patients.mat 中加载患者数据。

load patients
whos Smoker Weight
  Name          Size            Bytes  Class      Attributes

  Smoker      100x1               100  logical              
  Weight      100x1               800  double               

findgroups 指定组。G 的每个元素均为组编号,用于指定患者所在的组。组 1 包含非吸烟者,组 2 包含吸烟者。

G = findgroups(Smoker)
G = 100×1

     2
     1
     1
     1
     1
     1
     2
     1
     1
     1
      ⋮

显示患者的体重。

Weight
Weight = 100×1

   176
   163
   131
   133
   119
   142
   142
   180
   183
   132
      ⋮

使用 GWeight 数组分成两个体重组。应用 mean 函数。非吸烟者的平均体重略低于吸烟者的平均体重。

meanWeights = splitapply(@mean,Weight,G)
meanWeights = 2×1

  149.9091
  161.9412

计算各组患者的平均体重。在本例中,根据患者的吸烟或不吸烟状态以及就诊的医院对患者进行分组。数据集中有三家医院,因此有六组患者。

从示例文件 patients.mat 中加载患者的医院位置、是否为吸烟者以及患者体重。

load patients
whos Location Smoker Weight
  Name            Size            Bytes  Class      Attributes

  Location      100x1             14208  cell                 
  Smoker        100x1               100  logical              
  Weight        100x1               800  double               

显示 LocationSmoker 数组。

Location
Location = 100x1 cell
    {'County General Hospital'  }
    {'VA Hospital'              }
    {'St. Mary's Medical Center'}
    {'VA Hospital'              }
    {'County General Hospital'  }
    {'St. Mary's Medical Center'}
    {'VA Hospital'              }
    {'VA Hospital'              }
    {'St. Mary's Medical Center'}
    {'County General Hospital'  }
    {'County General Hospital'  }
    {'St. Mary's Medical Center'}
    {'VA Hospital'              }
    {'VA Hospital'              }
    {'St. Mary's Medical Center'}
    {'VA Hospital'              }
    {'St. Mary's Medical Center'}
    {'VA Hospital'              }
    {'County General Hospital'  }
    {'County General Hospital'  }
    {'VA Hospital'              }
    {'VA Hospital'              }
    {'VA Hospital'              }
    {'County General Hospital'  }
    {'County General Hospital'  }
    {'VA Hospital'              }
    {'VA Hospital'              }
    {'County General Hospital'  }
    {'County General Hospital'  }
    {'County General Hospital'  }
      ⋮

Smoker
Smoker = 100x1 logical array

   1
   0
   0
   0
   0
   0
   1
   0
   0
   0
      ⋮

根据位置和是否为吸烟者指定组。G 包含从 1 到 6 的整数,因为 SmokerLocation 中的值有六种可能的组合。

G = findgroups(Location,Smoker)
G = 100×1

     2
     5
     3
     5
     1
     3
     6
     5
     3
     1
      ⋮

计算每个组的平均体重。是否为吸烟者的患者体重差异要高于不同位置患者的体重差异。

meanWeights = splitapply(@mean,Weight,G)
meanWeights = 6×1

  150.1739
  159.8125
  146.8947
  158.4000
  152.0417
  165.9231

计算各患者组的体重均值,将结果显示在表中。要将体重均值与组 ID 相关联,请使用 findgroups 中的第二个输出参量。

从示例文件 patients.mat 中加载患者体重和是否为吸烟者。

load patients
whos Smoker Weight
  Name          Size            Bytes  Class      Attributes

  Smoker      100x1               100  logical              
  Weight      100x1               800  double               

使用 findgroups 指定组。输出参量 ID 中的值是 findgroups 根据分组变量找到的组的标签。

[G,ID] = findgroups(Smoker)
G = 100×1

     2
     1
     1
     1
     1
     1
     2
     1
     1
     1
      ⋮

ID = 2x1 logical array

   0
   1

计算体重均值。创建一个包含体重均值的表。

meanWeight = splitapply(@mean,Weight,G);
T = table(ID,meanWeight,'VariableNames',["Smokers","Mean Weights"])
T=2×2 table
    Smokers    Mean Weights
    _______    ____________

     false        149.91   
     true         161.94   

计算各患者组的体重均值,将结果显示在表中。在本例中,根据患者的吸烟或不吸烟状态以及就诊的医院对患者进行分组。

从示例文件 patients.mat 中加载患者的医院位置、是否为吸烟者以及患者体重。

load patients
whos Location Smoker Weight
  Name            Size            Bytes  Class      Attributes

  Location      100x1             14208  cell                 
  Smoker        100x1               100  logical              
  Weight        100x1               800  double               

Location 转换为字符串数组。然后使用位置和是否为吸烟者指定组。您可以指定两个组 ID 作为附加输出,因为您将两个分组变量指定为输入。位置和是否为吸烟者有六种可能的组合。ID1ID2 一起为六个组提供 ID。

Location = string(Location);
[G,ID1,ID2] = findgroups(Location,Smoker)
G = 100×1

     2
     5
     3
     5
     1
     3
     6
     5
     3
     1
      ⋮

ID1 = 6x1 string
    "County General Hospital"
    "County General Hospital"
    "St. Mary's Medical Center"
    "St. Mary's Medical Center"
    "VA Hospital"
    "VA Hospital"

ID2 = 6x1 logical array

   0
   1
   0
   1
   0
   1

计算每个组的平均体重。

meanWeights = splitapply(@mean,Weight,G)
meanWeights = 6×1

  150.1739
  159.8125
  146.8947
  158.4000
  152.0417
  165.9231

创建一个包含每组患者体重均值的表。

T = table(ID1,ID2,meanWeights,'VariableNames',["Hospital","Smoker","Mean Weight"])
T=6×3 table
             Hospital              Smoker    Mean Weight
    ___________________________    ______    ___________

    "County General Hospital"      false       150.17   
    "County General Hospital"      true        159.81   
    "St. Mary's Medical Center"    false       146.89   
    "St. Mary's Medical Center"    true         158.4   
    "VA Hospital"                  false       152.04   
    "VA Hospital"                  true        165.92   

使用表中的分组变量计算患者的体重均值。

将 100 名患者的医院位置和吸烟状态加载到表中。

load patients
T = table(Location,Smoker)
T=100×2 table
              Location               Smoker
    _____________________________    ______

    {'County General Hospital'  }    true  
    {'VA Hospital'              }    false 
    {'St. Mary's Medical Center'}    false 
    {'VA Hospital'              }    false 
    {'County General Hospital'  }    false 
    {'St. Mary's Medical Center'}    false 
    {'VA Hospital'              }    true  
    {'VA Hospital'              }    false 
    {'St. Mary's Medical Center'}    false 
    {'County General Hospital'  }    false 
    {'County General Hospital'  }    false 
    {'St. Mary's Medical Center'}    false 
    {'VA Hospital'              }    false 
    {'VA Hospital'              }    true  
    {'St. Mary's Medical Center'}    false 
    {'VA Hospital'              }    true  
      ⋮

使用 T 中的 SmokerLocation 变量指定各患者组。

G = findgroups(T)
G = 100×1

     2
     5
     3
     5
     1
     3
     6
     5
     3
     1
      ⋮

根据数据数组 Weight 计算体重均值。

meanWeights = splitapply(@mean,Weight,G)
meanWeights = 6×1

  150.1739
  159.8125
  146.8947
  158.4000
  152.0417
  165.9231

对于按医院位置及是否吸烟者分组的患者,创建一个包含体重均值的表。

将患者的位置和吸烟状态加载到表中。将 Location 转换为字符串数组。

load patients
Location = string(Location);
T = table(Location,Smoker)
T=100×2 table
             Location              Smoker
    ___________________________    ______

    "County General Hospital"      true  
    "VA Hospital"                  false 
    "St. Mary's Medical Center"    false 
    "VA Hospital"                  false 
    "County General Hospital"      false 
    "St. Mary's Medical Center"    false 
    "VA Hospital"                  true  
    "VA Hospital"                  false 
    "St. Mary's Medical Center"    false 
    "County General Hospital"      false 
    "County General Hospital"      false 
    "St. Mary's Medical Center"    false 
    "VA Hospital"                  false 
    "VA Hospital"                  true  
    "St. Mary's Medical Center"    false 
    "VA Hospital"                  true  
      ⋮

使用 T 中的 LocationSmoker 变量指定各患者组。输出表 TID 用于标识各组。

[G,TID] = findgroups(T);
TID
TID=6×2 table
             Location              Smoker
    ___________________________    ______

    "County General Hospital"      false 
    "County General Hospital"      true  
    "St. Mary's Medical Center"    false 
    "St. Mary's Medical Center"    true  
    "VA Hospital"                  false 
    "VA Hospital"                  true  

根据数据数组 Weight 计算体重均值。将体重均值追加到 TID

TID.meanWeight = splitapply(@mean,Weight,G)
TID=6×3 table
             Location              Smoker    meanWeight
    ___________________________    ______    __________

    "County General Hospital"      false       150.17  
    "County General Hospital"      true        159.81  
    "St. Mary's Medical Center"    false       146.89  
    "St. Mary's Medical Center"    true         158.4  
    "VA Hospital"                  false       152.04  
    "VA Hospital"                  true        165.92  

输入参数

全部折叠

分组变量,指定为向量。A 中的唯一值标识各组。您可以使用下表中列出的数据类型指定分组变量。

用于指定组的值

分组变量的数据类型

数字

数值或逻辑向量

文本

字符串数组或字符向量元胞数组

日期时间

datetimedurationcalendarDuration 向量

类别

categorical 向量

bin

分 bin 值的向量,通过对数值、datetimeduration 值的连续分布进行分 bin 而创建

分组变量,指定为表。findgroups 将每个表变量视为一个单独的分组变量。

表变量可以是数值、逻辑值、字符串、categoricaldatetimedurationcalendarDuration 向量,或是字符向量元胞数组。

输出参量

全部折叠

组编号,以正整数向量形式返回。对于分组变量中标识的 N 个组,介于 1 和 N 之间的每个整数指定一个组。当任何分组变量包含缺失字符串、空字符向量、NaNNaT 或未定义的 categorical 值时,G 会在相应位置包含 NaN

  • 如果分组变量为向量,则 G 和分组变量都具有相同的大小。

  • 如果分组变量在一个表中,则 G 的长度等于该表的行数。

用于标识每个组的值,以输入参量 A 中排序的唯一值的向量形式返回。ID 的数据类型与 A 的数据类型相同。

用于标识每个组的唯一值,以表形式返回。TID 的变量具有来自 T 的对应变量的经过排序的唯一值。但是,TIDT 不需要具有相同的行数。

详细信息

全部折叠

对数据组的计算

在数据分析中,您通常对数据组执行计算。对于这种计算,您可以将一个或多个数据变量拆分成若干数据组,对每个数据组执行计算,并将结果组合成一个或多个输出变量。您可以使用一个或多个分组变量来指定组。分组变量中的唯一值定义数据变量的对应值所属的组。

例如,该图显示简单的分组计算,该计算将 6×1 数值向量拆分为两组数据,计算每组数据的均值,然后将输出合并为一个 2×1 数值向量。6×1 分组变量有两个唯一值,即 ABXYZ

Calculation that splits a data variable based on a grouping variable, performs calculations on individual groups of data by applying the same function, and then concatenates the outputs of those function calls

您可以指定包含数值、文本、日期时间、类别或 bin 数据的分组变量。

扩展功能

版本历史记录

在 R2015b 中推出