主要内容

清洁表中的凌乱和缺少数据

This example shows how to find, clean, and delete table rows with missing data.

Load Sample Data

从逗号分隔的文本文件加载示例数据,MYSY.CSV。该文件包含许多不同的缺少数据指标:

  • Empty character vector ('')

  • 时期 (。)

  • NA

  • NaN

  • -99

要指定字符向量以将其视为空值,请使用'TreatAsMissing'name-value pair argument with the可读取function. (Use thedispfunction to display all 21 rows, even when running this example as a live script.)

t =可读取('Messy.csv','TreatAsMissing',{'.','na'}); disp(T)
A B C D E ________ ____ __________ ____ ____ {'afe1'} 3 {'yes' } 3 3 {'egh3'} NaN {'no' } 7 7 {'wth4'} 3 {'yes' } 3 3 {'atn2'} 23 {'no' } 23 23 {'arg1'} 5 {'yes' } 5 5 {'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 {0x0 char} 563 563 {'pnj5'} 463 {'no' } 463 463 {'wnn3'} 6 {'no' } 6 6 {'oks9'} 23 {'yes' } 23 23 {'wba3'} NaN {'yes' } NaN 14 {'pkn4'} 2 {'no' } 2 2 {'adw3'} 22 {'no' } 22 22 {'poj2'} -99 {'yes' } -99 -99 {'bas8'} 23 {'no' } 23 23 {'gry5'} NaN {'yes' } NaN 21

T是一张具有21行和5个变量的表。'TreatAsMissing'only applies to numeric columns in the file and cannot handle numeric values specified as text, such as'-99'

总结表

通过使用该表摘要创建表摘要,查看每个变量的数据类型,描述,单位和其他描述性统计信息summaryfunction.

summary(T)
变量:A:21x1字符矢量B:21x1双值:最小值-99中位数14最大563数字3 C:21x1字符矢量D:21x1双值:21x1双值:最小值-99中位数7 Max 7 Max 563 Nummissing 2 E:Nummissing 2 E:21x1双值:最小值-99中值14最大563

当您从文件导入数据时,默认值为可读取to read any variables with nonnumeric elements as a cell array of character vectors.

Find Rows with Missing Values

Display the subset of rows from the table,T, that have at least one missing value.

TF = ismissing(T,{'''.''na'nan -99});rowswithmissing = t(any(tf,2),:);disp(Rowswithmissing)
A B C D E ________ ___ __________ ___ ___ {'egh3'} NaN {'no' } 7 7 {'abk6'} 563 {0x0 char} 563 563 {'wba3'} NaN {'yes' } NaN 14 {'poj2'} -99 {'yes' } -99 -99 {'gry5'} NaN {'yes' } NaN 21

可读取replaced'.''na'withNaNin the numeric variables,B,D, 和E

Replace Missing Value Indicators

清洁数据,以使代码指示的缺失值-99have the standard MATLAB® numeric missing value indicator,NaN

t =标准化(t,-99);disp(t)
A B C D E ________ ____ __________ ____ ____ {'afe1'} 3 {'yes' } 3 3 {'egh3'} NaN {'no' } 7 7 {'wth4'} 3 {'yes' } 3 3 {'atn2'} 23 {'no' } 23 23 {'arg1'} 5 {'yes' } 5 5 {'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 {0x0 char} 563 563 {'pnj5'} 463 {'no' } 463 463 {'wnn3'} 6 {'no' } 6 6 {'oks9'} 23 {'yes' } 23 23 {'wba3'} NaN {'yes' } NaN 14 {'pkn4'} 2 {'no' } 2 2 {'adw3'} 22 {'no' } 22 22 {'poj2'} NaN {'yes' } NaN NaN {'bas8'} 23 {'no' } 23 23 {'gry5'} NaN {'yes' } NaN 21

standardizeMissingreplaces three instances of-99withNaN

创建一个新表格T2,并将缺少值替换为表的先前行中的值。fillmissing提供多种填充缺失值的方法。

T2 = fillmissing(T,'previous'); disp(T2)
A B C D E ________ ____ _______ ____ ____ {'afe1'} 3 {'yes'} 3 3 {'egh3'} 3 {'no' } 7 7 {'wth4'} 3 {'yes'} 3 3 {'atn2'} 23 {'no' } 23 23 {'arg1'} 5 {'yes'} 5 5 {'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 {'yes'} 563 563 {'pnj5'} 463 {'no' } 463 463 {'wnn3'} 6 {'no' } 6 6 {'oks9'} 23 {'yes'} 23 23 {'wba3'} 23 {'yes'} 23 14 {'pkn4'} 2 {'no' } 2 2 {'adw3'} 22 {'no' } 22 22 {'poj2'} 22 {'yes'} 22 22 {'bas8'} 23 {'no' } 23 23 {'gry5'} 23 {'yes'} 23 21

删除缺少值的行

创建一个新表格T3, that contains only the rows fromT没有缺少值。T3只有16行。

t3 = rmmissing(t);disp(T3)
A B C D E ________ ____ _______ ____ ____ {'afe1'} 3 {'yes'} 3 3 3 {'wth4'} 3 {'yes'} 3 3 3 3 {'atn2'} 23 {'no'5 {'yes'} 5 5 {'jre3'} 34.6 {'yes'} 34.6 34.6 {'wen9'} 234 {'yes'} 234 234 234 {'ple2'} 2'} 5 {'no'} 5 5 {'oii4'} 5 {'yes'} 5 5 {'wnk3'} 245 {'yes'} 245 245 {'pnj5'} 463 {'no'} 463 463 463 463 {'wnn3'} 6 {'no'} 6 6 {'oks9'} 23 {'yes'} 23 23 {'pkn4'} 2 {'no'} 2 2 2 {'adw3'} 22 {'no''} 22 22 2222 {'bas8'} 23 {'no'} 23 23 23

T3包含16行和5个变量。

Organize Data

排序T3按顺序降顺序C, 和then sort in ascending order byA

t3 = sortrows(t2,{'C','A'},{'descend','ascend'}); disp(T3)
A B C D E ________ ____ _______ ____ ____ {'abk6'} 563 {'YES'} 563 563 {'afe1'} 3 {'YES'} 3 3 3 {'arg1'}23 {'yes'} 23 21 {'jre3'} 34.6 {'yes'} 34.6 34.6 {'oii4'} 5 {'yes'} 5 5 5 {'oks9'} 23 {'yes'yes''Y YES'} 23 23 23 23 {'poj2 {'poj2'} 22 {'yes'} 22 22 {'wba3'} 23 {'yes'} 23 14 {'wen9'} 234 {'yes'} 234 234 234 {'wnk3'} 245 {'yes'yes'Y YES'} 245 245 245 {'wth4'} 3 {'yes'} 3 3 {'adw3'} 22 {'no'} 22 22 22 {'atn2'} 23 {'no'} 23 23 23 {'bas8'} 23 {'no'} 2323 {'dbo8'} 5 {'no'} 5 5 {'egh3'} 3 {'no'} 7 7 7 {'pkn4'} 2 {'no'} 2 2 2 {'ple2'} 2 {'no'''} 2 2 {'pnj5'} 463 {'no'} 463 463 {'wnn3'} 6 {'no'} 6 6 6

C,排首先分组'yes', followed by'no'。Then inA, the rows are listed alphabetically.

Reorder the table so thatAC彼此相邻。

T3 = T3(:,{'A','C','B','D','E'}); disp(T3)
A C B D E ________ _______ ____ ____ ____ {'abk6'} {'YES'} 563 563 563 {'afe1'} {'YES'} 3 3 3 3 3 3 3 3 {'arg1'}'y yes'YES'}{'YES'} 23 23 23 21 {'jre3'} {'YES'} 34.6 34.6 34.6 34.6 {'oii4'} {'YES'} 5 5 5 5 5 5 5 5 {'oks9'} {'yes'YES'} 23 23 23 23 23 23 23 23 {'poj2 {'poj2'} {'yes'} 22 22 22 {'wba3'} {'yes'} 23 23 14 {'wen9'} {'yes'} 234 234 234 234 {'wnk3'} {'yes'Y YES'} 245 245 245 245 {'wth4'} {'yes'} 3 3 3 3 {'adw3'} {'no'} 22 22 22 22 22 {'atn2'} {'no'} 23 23 23 23 23 {'bas8'} {'no'} 23 23 23 23 23 2323 {'dbo8'} {'no'} 5 5 5 5 {'egh3'} {'no'} 3 7 7 7 {'pkn4'} {'no'} 2 2 2 2 2 2 {'ple2'} {'no'''}2 2 2 2 {'pnj5'} {'no'} 463 463 463 {'wnn3'} {'no'} 6 6 6 6 6 6 6 6

See Also

||||||

相关话题