主要内容

添加和删除表行

此示例演示了如何在表中添加和删除行。您可以在底部添加行,在中间插入行,或合并两个表。如果您为表预分配空间,则可以通过替换行来用数据填充它。您可以按编号、按名称或按条件删除行,包括重复行或有缺失值的行。对于简单情况,您可以使用Variables Editor以交互方式修改表。

创建示例表

首先,使用 readtable 函数从逗号分隔值 (CSV) 文件创建一个表。该示例文件包含关于少量患者的仿真数据。第三行有缺失值,表示该患者的数据不完整。

patientSample1 = readtable("patientSample1.csv",TextType="string")
patientSample1=5×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Martinez"    37       70       179        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            

在表末尾添加行

要向表末尾添加一行,请使用单行表或单行元胞数组。通过将新行放在现有行索引边界之外来添加新行。

为方便起见,请使用包含新行值的元胞数组。包含每个表变量的值,并使用兼容的数据类型。

oneRowCellArray = {"Lee" 55 73 167 false "Fair"}
oneRowCellArray=1×6 cell array
    {["Lee"]}    {[55]}    {[73]}    {[167]}    {[0]}    {["Fair"]}

要将行添加到表底部,请将 end+1 指定为行索引。通常,end 关键字指定数组的最后一个元素。当用于指定数组或表的行索引时,end 指定最后一行。该赋值从元胞数组中提取值,并将它们赋给 patientSample1 的新行。

patientSample1(end+1,:) = oneRowCellArray
patientSample1=6×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Martinez"    37       70       179        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            

同样,您可以使用单行表,无论是通过创建新表还是指定现有表的一行。

创建一个单行表。

oneRowTable = table("Griffin",49,70,186,false,"Fair")
oneRowTable=1×6 table
      Var1       Var2    Var3    Var4    Var5      Var6 
    _________    ____    ____    ____    _____    ______

    "Griffin"     49      70     186     false    "Fair"

当您使用此语法对一行赋值时,patientSample1oneRowTable 的变量的名称可以不同。在此语法中,赋值从 oneRowTable 中提取值,并将它们赋到 patientSample1 的新行中。

patientSample1(end+1,:) = oneRowTable
patientSample1=7×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Martinez"    37       70       179        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Griffin"     49       70       186        0             "Fair"            

替换现有行

您也可以为已存在的行赋值。赋值运算会覆盖该行。

例如,将 oneRowCellArray 赋给 patientSample1 的第二行。该赋值运算用患者 Lee 的数据覆盖患者 Martinez 的数据。

patientSample1(2,:) = oneRowCellArray
patientSample1=7×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Brown"      49       64       119        0             "Good"            
    "Lee"        55       73       167        0             "Fair"            
    "Chen"       55      NaN       NaN        1             <missing>         
    "Wright"     45       70       126        1             "Excellent"       
    "Kim"        41       65       127        0             "Poor"            
    "Lee"        55       73       167        0             "Fair"            
    "Griffin"    49       70       186        0             "Fair"            

在特定位置插入行

要在现有行之间的特定位置插入一行,请将表拆分为两部分并使用垂直串联。例如,在表的第三行和第四行之间插入 oneRowCellArray

patientSample1 = [patientSample1(1:3,:); oneRowCellArray; patientSample1(4:end,:)]
patientSample1=8×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Brown"      49       64       119        0             "Good"            
    "Lee"        55       73       167        0             "Fair"            
    "Chen"       55      NaN       NaN        1             <missing>         
    "Lee"        55       73       167        0             "Fair"            
    "Wright"     45       70       126        1             "Excellent"       
    "Kim"        41       65       127        0             "Poor"            
    "Lee"        55       73       167        0             "Fair"            
    "Griffin"    49       70       186        0             "Fair"            

添加行并对表排序

插入行的另一种方法是将行添加到表末尾,然后使用 sortrows 函数对表重新排序。

例如,为另一位患者添加一行。

patientSample1(end+1,:) = {"Anderson" 45 68 128 false "Excellent"}
patientSample1=9×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Brown"       49       64       119        0             "Good"            
    "Lee"         55       73       167        0             "Fair"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Griffin"     49       70       186        0             "Fair"            
    "Anderson"    45       68       128        0             "Excellent"       

如果您的表是无序的,或在添加新行后变得无序,请使用 sortrows 对表进行排序。默认情况下,sortrows 按第一个变量中的值升序对表进行排序。在本例中,排序顺序是按患者的姓氏。

patientSample1 = sortrows(patientSample1)
patientSample1=9×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Brown"       49       64       119        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       

向空表添加行

您可以从空表开始,然后向其中添加数据行。如果您随着时间的推移收集数据并希望周期性地将数据添加到表末尾,此方法可能很有用。

首先,创建一个空表。

startFromEmpty = table
startFromEmpty =

  0×0 empty table

接下来,通过在表的最后一行后添加一行来扩展表。

startFromEmpty(end+1,:) = oneRowCellArray
startFromEmpty=1×6 table
    Var1     Var2    Var3    Var4    Var5      Var6 
    _____    ____    ____    ____    _____    ______

    "Lee"     55      73     167     false    "Fair"

要重命名所有变量,请将新变量名称数组赋给表的 VariableNames 属性。

varNames = ["LastName" "Age" "Height" "Weight" "Smoker" "SelfAssessedHealthStatus"];
startFromEmpty.Properties.VariableNames = varNames
startFromEmpty=1×6 table
    LastName    Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    ________    ___    ______    ______    ______    ________________________

     "Lee"      55       73       167      false              "Fair"         

将行赋给预分配的表

虽然您可以从空表开始并一次添加一行,但预分配一个包含多行的表可能更高效。然后,您可以在数据可用时填充行。

要预分配表,请使用 table 函数。指定表的大小、变量的名称以及变量的数据类型。例如,预分配一个包含六行和九个变量的表。预分配会用适合变量数据类型的缺失值填充变量。

varNames = ["LastName" "Age" "Height" "Weight" "Smoker" "SelfAssessedHealthStatus"];
varTypes = ["string" "double" "double" "double" "logical" "string"];
preallocatedTable = table(Size=[4 6],VariableNames=varNames,VariableTypes=varTypes)
preallocatedTable=4×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    <missing>     0       0         0       false            <missing>        
    <missing>     0       0         0       false            <missing>        
    <missing>     0       0         0       false            <missing>        
    <missing>     0       0         0       false            <missing>        

将数据添加到第一行。此赋值会覆盖第一行中的缺失值。

preallocatedTable(1,:) = oneRowCellArray
preallocatedTable=4×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Lee"        55       73       167      false            "Fair"           
    <missing>     0        0         0      false            <missing>        
    <missing>     0        0         0      false            <missing>        
    <missing>     0        0         0      false            <missing>        

添加空行

从 R2023b 开始,您可以使用 resize 函数在表末尾预分配更多行。新行用缺失值填充,您可以稍后更新这些值。

首先,使用 height 函数确定 patientSample1 有多少行。

numberOfRows = height(patientSample1)
numberOfRows = 
9

接下来,使用 resize 另外添加三行。resize 的第二个参量指定调整大小后的表中的总行数。

patientSample1 = resize(patientSample1,numberOfRows+3)
patientSample1=12×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Brown"       49       64       119        0             "Good"            
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Kim"         41       65       127        0             "Poor"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         

合并两个表

向表添加行的另一种方法是串联另一个表。当您垂直串联两个表时,它们必须具有相同的行数和相同的变量名称。来自顶部表和底部表的匹配变量必须具有兼容的数据类型和大小。但是,两个表的变量顺序可以不同,因为垂直串联按名称匹配变量。串联后的表具有与顶部表相同的变量顺序。

例如,从另一个示例 CSV 文件创建第二个表。第二个表与第一个表具有相同的变量。

patientSample2 = readtable("patientSample2.csv",TextType="string")
patientSample2=4×6 table
     LastName      Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    ___________    ___    ______    ______    ______    ________________________

    "Garcia"       27       69       131        1             "Fair"            
    "Murphy"       36       71       180        0             "Good"            
    "Takahashi"    29       63       130        0             "Excellent"       
    "Brown"        49       64       119        0             "Good"            

垂直串联这两个表。

combinedPatients = [patientSample1; patientSample2]
combinedPatients=16×6 table
     LastName      Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    ___________    ___    ______    ______    ______    ________________________

    "Anderson"     45       68       128        0             "Excellent"       
    "Brown"        49       64       119        0             "Good"            
    "Chen"         55      NaN       NaN        1             <missing>         
    "Griffin"      49       70       186        0             "Fair"            
    "Kim"          41       65       127        0             "Poor"            
    "Lee"          55       73       167        0             "Fair"            
    "Lee"          55       73       167        0             "Fair"            
    "Lee"          55       73       167        0             "Fair"            
    "Wright"       45       70       126        1             "Excellent"       
    <missing>       0        0         0        0             <missing>         
    <missing>       0        0         0        0             <missing>         
    <missing>       0        0         0        0             <missing>         
    "Garcia"       27       69       131        1             "Fair"            
    "Murphy"       36       71       180        0             "Good"            
    "Takahashi"    29       63       130        0             "Excellent"       
    "Brown"        49       64       119        0             "Good"            

按行号删除行

您可以通过使用行号作为下标并为空数组 [] 赋值来删除行。

例如,从表中删除第 2 行和第 5 行。

patientSample1([2 5],:) = []
patientSample1=10×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         

删除重复行

您也可以通过删除重复行来清理表。

例如,使用 unique 函数删除名为 Lee 的患者的重复行。该函数不会删除末尾填充有缺失值的行,因为缺失值被视为唯一值。缺失值不等于任何值,甚至不等于自身。

patientSample1 = unique(patientSample1)
patientSample1=8×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Chen"        55      NaN       NaN        1             <missing>         
    "Griffin"     49       70       186        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         
    <missing>      0        0         0        0             <missing>         

删除具有缺失值的行

如果表包含有缺失值的行,您可以使用 rmmissing 函数删除这些行。

例如,从患者数据表中删除有缺失值的表行。

patientSample1 = rmmissing(patientSample1)
patientSample1=4×6 table
     LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    __________    ___    ______    ______    ______    ________________________

    "Anderson"    45       68       128        0             "Excellent"       
    "Griffin"     49       70       186        0             "Fair"            
    "Lee"         55       73       167        0             "Fair"            
    "Wright"      45       70       126        1             "Excellent"       

您也可以使用 fillmissing 函数填充缺失值。

删除满足条件的行

您也可以删除一个或多个变量中的值满足条件的行。

例如,查找年龄不超过 45 岁的患者的行。<= 运算符返回一个逻辑向量,您可以将其用作行下标。

ageLessThanOrEqualTo45 = patientSample1.Age <= 45
ageLessThanOrEqualTo45 = 4×1 logical array

   1
   0
   0
   1

删除 Age 小于等于 45 的行。

patientSample1(ageLessThanOrEqualTo45,:) = []
patientSample1=2×6 table
    LastName     Age    Height    Weight    Smoker    SelfAssessedHealthStatus
    _________    ___    ______    ______    ______    ________________________

    "Griffin"    49       70       186        0                "Fair"         
    "Lee"        55       73       167        0                "Fair"         

按行名称删除行

当表具有行名称时,您可以使用行名称作为下标。为 patientSample1 指定行名称。然后通过行名称删除一行。

首先,将标识符变量 LastName 指定为行名称。然后,从 patientSample1 中删除变量 LastName

patientSample1.Properties.RowNames = patientSample1.LastName;
patientSample1.LastName = []
patientSample1=2×5 table
               Age    Height    Weight    Smoker    SelfAssessedHealthStatus
               ___    ______    ______    ______    ________________________

    Griffin    49       70       186        0                "Fair"         
    Lee        55       73       167        0                "Fair"         

通过使用行名称作为下标并为空数组赋值 [] 来删除一行。

patientSample1("Lee",:) = []
patientSample1=1×5 table
               Age    Height    Weight    Smoker    SelfAssessedHealthStatus
               ___    ______    ______    ______    ________________________

    Griffin    49       70       186        0                "Fair"         

另请参阅

函数

工具

主题