テーブル行の追加と削除
この例では、テーブルで行を追加および削除する方法を示します。下に行を追加したり、間に行を挿入したり、2 つの table を結合したりできます。table にスペースを事前に割り当てると、行を置き換えることでそのスペースにデータを埋めることができます。重複している行や欠損値のある行などは、番号、名前、または条件を使って行を削除できます。シンプルなケースでは、Variables Editorを使用して table を対話的に変更できます。
サンプル table の作成
まず、readtable 関数を使用して、コンマ区切り値 (CSV) ファイルから table を作成します。サンプル ファイルには、少数の患者に関するシミュレートされたデータが含まれています。3 行目に欠損値があり、その患者のデータが不完全であることを示しています。
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"
table の最後への行の追加
table の最後に 1 行を追加するには、1 行の table または 1 行の cell 配列を使用します。新しい行を既存の行インデックス境界の外側に配置して追加します。
簡便のために、新しい行の値を含む cell 配列を使用します。互換性のあるデータ型を使用して、各 table 変数の値を含めます。
oneRowCellArray = {"Lee" 55 73 167 false "Fair"}oneRowCellArray=1×6 cell array
{["Lee"]} {[55]} {[73]} {[167]} {[0]} {["Fair"]}
table の下に行を追加するには、行インデックスとして end+1 を指定します。一般に、end キーワードは配列の最後の要素を指定します。配列または table の行インデックスを指定するために使用した場合、end は最後の行を指定します。この代入では、cell 配列から値を抽出して 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"
同様に、新しい table を作成するか、既存の table の 1 行を指定することで、1 行の table を使用できます。
1 行の table を作成します。
oneRowTable = table("Griffin",49,70,186,false,"Fair")
oneRowTable=1×6 table
Var1 Var2 Var3 Var4 Var5 Var6
_________ ____ ____ ____ _____ ______
"Griffin" 49 70 186 false "Fair"
この構文を使用して行に代入する場合、patientSample1 と oneRowTable の変数の名前は異なっていてもかまいません。この構文の代入では、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"
既存の行の置換
既に存在する行に値を代入することもできます。代入操作によって行が上書きされます。
たとえば、patientSample1 の 2 行目に oneRowCellArray を代入します。この代入により、患者 Martinez のデータが患者 Lee のデータで上書きされます。
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"
特定の位置への行の挿入
既存の行の間にある特定の位置に行を挿入するには、table を 2 つに分割し、垂直連結を使用します。たとえば、table の 3 行目と 4 行目の間に 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"
行の追加と table の並べ替え
行を挿入するもう 1 つの方法として、table の最後に行を追加してから、sortrows 関数を使用して table を並べ替える方法があります。
たとえば、別の患者の行を追加します。
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"
table が順序付けられていない場合、または新しい行を追加して順序が正しくなくなった場合は、sortrows を使用して table を並べ替えます。既定では、sortrows は最初の変数の値の昇順で table を並べ替えます。この場合、並べ替え順序は患者の姓順になります。
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"
空の table への行の追加
空の table から開始して、その table にデータ行を追加できます。この方法は、長期にわたってデータを収集し、table の最後にデータを定期的に追加する場合に役立ちます。
まず、空の table を作成します。
startFromEmpty = table
startFromEmpty = 0×0 empty table
次に、table の最後の行の後に行を追加して table を拡張します。
startFromEmpty(end+1,:) = oneRowCellArray
startFromEmpty=1×6 table
Var1 Var2 Var3 Var4 Var5 Var6
_____ ____ ____ ____ _____ ______
"Lee" 55 73 167 false "Fair"
すべての変数の名前を変更するには、新しい変数名から成る配列を table の 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 への行の代入
空の table から開始して一度に 1 行ずつ行を追加できますが、多くの行を含む table を事前に割り当てる方が効率的な場合があります。その後、データが利用可能になったときに行を埋めることができます。
table を事前に割り当てるには、table 関数を使用します。table のサイズ、変数の名前、および変数のデータ型を指定します。たとえば、6 行と 9 つの変数を含む 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 関数を使用して table の最後に追加行を事前に割り当てることができます。新しい行は欠損値で埋められますが、後で更新できます。
まず、height 関数を使用して、patientSample1 に含まれている行数を判別します。
numberOfRows = height(patientSample1)
numberOfRows = 9
次に、resize を使用してさらに 3 行を追加します。resize の 2 番目の引数では、サイズ変更後の table の合計行数を指定します。
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>
2 つの table の結合
table に行を追加する別の方法として、別の table を連結する方法があります。2 つの table を垂直方向に連結する場合、両方の table の変数の数が同じで、変数名も同じでなければなりません。上の table と下の table の対応する変数のデータ型とサイズは互換性がなければなりません。ただし、垂直連結では変数が名前で対応付けられるため、2 つの table で変数の順序が異なっていてもかまいません。連結後の table における変数の順序は、上の table と同じになります。
たとえば、別のサンプル CSV ファイルから 2 番目の table を作成します。2 番目の table には、最初の table と同じ変数があります。
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"
2 つの table を垂直方向に連結します。
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"
行番号による行の削除
行番号を添字として使用し、空の配列 [] を代入することで、行を削除できます。
たとえば、table から 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>
重複行の削除
重複行を削除して table をクリーンアップすることもできます。
たとえば、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>
欠損値を含む行の削除
table に欠損値を含む行がある場合は、rmmissing 関数を使用して該当する行を削除できます。
たとえば、欠損値を含む table 行を患者データ table から削除します。
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 関数を使用して欠損値に値を埋めることもできます。
条件を満たす行の削除
1 つ以上の変数の値が条件を満たす行を削除することもできます。
たとえば、45 歳以下の患者の行を求めます。<= 演算子は、行の添字として使用できる logical ベクトルを返します。
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"
行名による行の削除
table に行名がある場合、行名を添字として使用できます。patientSample1 に行名を指定します。次に、行名によって行を削除します。
まず、識別子の変数 LastName を行名として指定します。次に、変数 LastName を patientSample1 から削除します。
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"