table 内の乱雑な欠損データの整理
この例では、データ値が乱雑で欠損している table を整理して再編成する方法を示します。まず、インポート ツールまたは関数 summary
や ismissing
などの関数を使用して欠損データを特定できます。関数 standardizeMissing
、fillmissing
、または rmmissing
を使用して欠損値を標準化するか、埋めるか、削除できます。次に、table を再編成してさらに整理できます。関数 sortrows
や movevars
などの関数を使用して、table の行と変数を再配列できます。
ファイル内のデータの調査
インポート ツールを使用して、サンプルのコンマ区切り値 (CSV) ファイル messy.csv
内のデータを調べます。ツールではデータのプレビューが表示され、データをインポートする方法を指定できます。インポート ツールで messy.csv
を調べるには、MATLAB® でこの例を開いた後、[ホーム] タブに移動し、[変数] セクションの [データのインポート] をクリックし、ファイル選択ダイアログ ボックスを使用して messy.csv
を開きます。
インポート ツールで、テキストと数値をもつ 5 つの列が messy.csv
に含まれていることが示されます。
このファイルには、多くの異なる欠損データ インジケーターが含まれています。
空のテキスト
ピリオド (.)
NA
NaN
-99
インポート ツールでは、数値列の NaN
やテキスト列の空のテキストなどの一部の欠損データ インジケーターが自動的に認識されます (ただし、視覚的に強調表示はされません)。
ツールでは、列 B
に含まれている空のテキスト、ピリオド、NA
などのその他のインジケーターは強調表示されます。これらの値は標準の欠損値ではありません。しかし、数値列の非数値は、欠損値を表すものである可能性が高くなっています。データをインポートする際に、これらの値も欠損値として扱うように指定できます。
-99
などの単一の負の値を除いて数値データが正の値からなる場合、その負の値を欠損データのフラグにすることができます。ご使用の table で -99
などの数値が欠損データを表す場合は、table を整理する際にその数値が欠損値であると指定する必要があります。
table としてのデータのインポート
インポート ツールからデータを MATLAB ワークスペースにインポートできます。関数 readtable
を使用してデータをファイルから読み取って table としてインポートすることもできます。
関数 readtable
を使用して、messy.csv
内のデータをインポートします。テキスト データを string 配列である table 変数に読み取るには、名前と値の引数 TextType
を使用します。数値列内の指定した非数値を欠損値として扱うには、名前と値の引数 TreatAsMissing
を使用します。テキスト データを含む列 A
および C
では、readtable
は空のテキストを欠損 string としてインポートします。欠損 string は <missing>
と表示されます。数値データを含む列 B
、D
、および E
では、readtable
は空のテキストを NaN
値としてインポートします。また、.
および NA
についても、TreatAsMissing
を使用して指定した場合は NaN
値としてインポートします。ただし、値 -99
は数値であるため変更されません。
messyTable = readtable("messy.csv","TextType","string","TreatAsMissing",[".","NA"])
messyTable=21×5 table
A B C D E
______ ____ _________ ____ ____
"afe1" NaN "yes" 3 3
"egh3" NaN "no" 7 7
"wth4" -99 "yes" -99 -99
"atn2" 23.7 <missing> NaN 23.7
"arg1" NaN "yes" 5 NaN
"jre3" 34.6 "yes" 34.6 34.6
"wen9" 234 "yes" 234 234
"ple2" 2 "no" 2 2
"dbo8" 5 "no" 5 5
"oii4" 5 "yes" 5 5
"wnk3" 245 "yes" 245 245
"abk6" 563 "no" 563 563
"pnj5" 463 "no" 463 463
"wnn3" 6 "no" 6 6
"oks9" 23 "yes" 23 23
"wba3" 14 "yes" 14 14
⋮
table の概要の表示
table の概要を表示するには、関数 summary
を使用します。概要では、table 変数ごとに、データ型およびその他の記述統計が表示されます。たとえば、summary
では、messyTable
の各数値変数に含まれている欠損値の数が表示されます。
summary(messyTable)
Variables: A: 21x1 string B: 21x1 double Values: Min -99 Median 22.5 Max 563 NumMissing 3 C: 21x1 string D: 21x1 double Values: Min -99 Median 14 Max 563 NumMissing 2 E: 21x1 double Values: Min -99 Median 21.5 Max 563 NumMissing 1
欠損値がある行の検出
1 つ以上の欠損値が含まれている messyTable
の行を検出するには、関数 ismissing
を使用します。データに -99
などの非標準欠損値が含まれている場合は、標準欠損値とともにその非標準欠損値を指定できます。
ismissing
の出力は、欠損値が含まれている messyTable
の要素を識別する logical 配列です。
missingElements = ismissing(messyTable,{string(missing),NaN,-99})
missingElements = 21x5 logical array
0 1 0 0 0
0 1 0 0 0
0 1 0 1 1
0 0 1 1 0
0 1 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
⋮
欠損値が含まれている行を識別する logical ベクトルを作成するには、関数 any
を使用します。
rowsWithMissingValues = any(missingElements,2)
rowsWithMissingValues = 21x1 logical array
1
1
1
1
1
0
0
0
0
0
⋮
table へのインデックス付けを行って欠損値が含まれている行のみを返すには、logical ベクトル rowsWithMissingValues
を使用します。
missingValuesTable = messyTable(rowsWithMissingValues,:)
missingValuesTable=6×5 table
A B C D E
______ ____ _________ ___ ____
"afe1" NaN "yes" 3 3
"egh3" NaN "no" 7 7
"wth4" -99 "yes" -99 -99
"atn2" 23.7 <missing> NaN 23.7
"arg1" NaN "yes" 5 NaN
"gry5" 21 "yes" NaN 21
欠損値を埋める
table で欠損値を整理する 1 つの方法は、欠損値を意味のある値に置き換えることです。標準欠損値を挿入することで、非標準欠損値を置き換えることができます。次に、欠損値を調整値で埋めることができます。たとえば、欠損値を最近傍値や table 変数の平均値で埋めることができます。
この例では、-99
は欠損値を示す非標準値です。-99
のインスタンスを標準欠損値で置き換えるために、関数 standardizeMissing
を使用します。NaN
は単精度および倍精度浮動小数点数値配列の標準欠損値です。
messyTable = standardizeMissing(messyTable,-99)
messyTable=21×5 table
A B C D E
______ ____ _________ ____ ____
"afe1" NaN "yes" 3 3
"egh3" NaN "no" 7 7
"wth4" NaN "yes" NaN NaN
"atn2" 23.7 <missing> NaN 23.7
"arg1" NaN "yes" 5 NaN
"jre3" 34.6 "yes" 34.6 34.6
"wen9" 234 "yes" 234 234
"ple2" 2 "no" 2 2
"dbo8" 5 "no" 5 5
"oii4" 5 "yes" 5 5
"wnk3" 245 "yes" 245 245
"abk6" 563 "no" 563 563
"pnj5" 463 "no" 463 463
"wnn3" 6 "no" 6 6
"oks9" 23 "yes" 23 23
"wba3" 14 "yes" 14 14
⋮
欠損値を埋めるには、関数 fillmissing
を使用します。この関数では、欠損値を埋める多くの方法が用意されています。たとえば、欠損値を、欠損値ではない最近傍値で埋めます。
filledTable = fillmissing(messyTable,"nearest")
filledTable=21×5 table
A B C D E
______ ____ _____ ____ ____
"afe1" 23.7 "yes" 3 3
"egh3" 23.7 "no" 7 7
"wth4" 23.7 "yes" 7 23.7
"atn2" 23.7 "yes" 5 23.7
"arg1" 34.6 "yes" 5 34.6
"jre3" 34.6 "yes" 34.6 34.6
"wen9" 234 "yes" 234 234
"ple2" 2 "no" 2 2
"dbo8" 5 "no" 5 5
"oii4" 5 "yes" 5 5
"wnk3" 245 "yes" 245 245
"abk6" 563 "no" 563 563
"pnj5" 463 "no" 463 463
"wnn3" 6 "no" 6 6
"oks9" 23 "yes" 23 23
"wba3" 14 "yes" 14 14
⋮
欠損値がある行の削除
table で欠損値を整理するもう 1 つの方法は、欠損値が含まれている行を削除することです。
欠損値が含まれている行を削除するには、関数 rmmissing
を使用します。
remainingTable = rmmissing(messyTable)
remainingTable=15×5 table
A B C D E
______ ____ _____ ____ ____
"jre3" 34.6 "yes" 34.6 34.6
"wen9" 234 "yes" 234 234
"ple2" 2 "no" 2 2
"dbo8" 5 "no" 5 5
"oii4" 5 "yes" 5 5
"wnk3" 245 "yes" 245 245
"abk6" 563 "no" 563 563
"pnj5" 463 "no" 463 463
"wnn3" 6 "no" 6 6
"oks9" 23 "yes" 23 23
"wba3" 14 "yes" 14 14
"pkn4" 2 "no" 2 2
"adw3" 22 "no" 22 22
"poj2" 34.6 "yes" 34.6 34.6
"bas8" 23 "no" 23 23
table 行と変数の並べ替えと再配列
table で欠損値を整理した後に、他の方法で編成できます。たとえば、1 つ以上の変数の値で table の行を並べ替えることができます。
最初の変数 A
の値で行を並べ替えます。
sortedTable = sortrows(remainingTable)
sortedTable=15×5 table
A B C D E
______ ____ _____ ____ ____
"abk6" 563 "no" 563 563
"adw3" 22 "no" 22 22
"bas8" 23 "no" 23 23
"dbo8" 5 "no" 5 5
"jre3" 34.6 "yes" 34.6 34.6
"oii4" 5 "yes" 5 5
"oks9" 23 "yes" 23 23
"pkn4" 2 "no" 2 2
"ple2" 2 "no" 2 2
"pnj5" 463 "no" 463 463
"poj2" 34.6 "yes" 34.6 34.6
"wba3" 14 "yes" 14 14
"wen9" 234 "yes" 234 234
"wnk3" 245 "yes" 245 245
"wnn3" 6 "no" 6 6
行を C
の降順に並べ替え、次に A
の昇順に並べ替えます。
sortedBy2Vars = sortrows(remainingTable,["C","A"],["descend","ascend"])
sortedBy2Vars=15×5 table
A B C D E
______ ____ _____ ____ ____
"jre3" 34.6 "yes" 34.6 34.6
"oii4" 5 "yes" 5 5
"oks9" 23 "yes" 23 23
"poj2" 34.6 "yes" 34.6 34.6
"wba3" 14 "yes" 14 14
"wen9" 234 "yes" 234 234
"wnk3" 245 "yes" 245 245
"abk6" 563 "no" 563 563
"adw3" 22 "no" 22 22
"bas8" 23 "no" 23 23
"dbo8" 5 "no" 5 5
"pkn4" 2 "no" 2 2
"ple2" 2 "no" 2 2
"pnj5" 463 "no" 463 463
"wnn3" 6 "no" 6 6
C
での並べ替えにより、"yes"
でグループ化された行の次に、"no"
でグループ化された行が並べられています。次に、A
での並べ替えにより、さらにその行がアルファベット順に並べられています。
A
と C
が隣同士になるように table を並べ替えるために、関数 movevars
を使用します。
sortedRowsAndMovedVars = movevars(sortedBy2Vars,"C","After","A")
sortedRowsAndMovedVars=15×5 table
A C B D E
______ _____ ____ ____ ____
"jre3" "yes" 34.6 34.6 34.6
"oii4" "yes" 5 5 5
"oks9" "yes" 23 23 23
"poj2" "yes" 34.6 34.6 34.6
"wba3" "yes" 14 14 14
"wen9" "yes" 234 234 234
"wnk3" "yes" 245 245 245
"abk6" "no" 563 563 563
"adw3" "no" 22 22 22
"bas8" "no" 23 23 23
"dbo8" "no" 5 5 5
"pkn4" "no" 2 2 2
"ple2" "no" 2 2 2
"pnj5" "no" 463 463 463
"wnn3" "no" 6 6 6
インデックス付けによって table 変数を並べ替えることもできます。小かっこと、出力 table 内の変数の順序を指定する変数インデックスを使用します。
sortedRowsAndMovedVars = sortedBy2Vars(:,["A","C","B","D","E"])
sortedRowsAndMovedVars=15×5 table
A C B D E
______ _____ ____ ____ ____
"jre3" "yes" 34.6 34.6 34.6
"oii4" "yes" 5 5 5
"oks9" "yes" 23 23 23
"poj2" "yes" 34.6 34.6 34.6
"wba3" "yes" 14 14 14
"wen9" "yes" 234 234 234
"wnk3" "yes" 245 245 245
"abk6" "no" 563 563 563
"adw3" "no" 22 22 22
"bas8" "no" 23 23 23
"dbo8" "no" 5 5 5
"pkn4" "no" 2 2 2
"ple2" "no" 2 2 2
"pnj5" "no" 463 463 463
"wnn3" "no" 6 6 6
参考
table
| ismissing
| standardizeMissing
| fillmissing
| rmmissing
| sortrows
| movevars
| Import Tool
| readtable
| summary