フィルターのクリア

Replace NaN's in table with zero

366 ビュー (過去 30 日間)
xander fong
xander fong 2015 年 7 月 24 日
コメント済み: hager fouda 2023 年 9 月 1 日
Hello, I have a 1501x7 table called 'x' and there appears to be NaN's in the fourth and sixth column called "Age" and "height". I would like a way to replace NaN's with zeros. Take note, that I have already tried:
k = find(isnan(x))';
x(k) = 0;
% and
x(isnan(x)) = 0;
Yet, neither work because I am using a table, not a matrix. I have also tried converting my table into a cell array, and using these same functions, but they still do not work. They return:"Undefined function 'isnan' for input arguments of type 'cell'" ALSO, please note that the table has columns full of text. So, cell2mat does not work.
  3 件のコメント
xander fong
xander fong 2015 年 7 月 27 日
I get this: "Undefined function 'isnan' for input arguments of type 'table'.
hager fouda
hager fouda 2023 年 9 月 1 日
Thank you so much. it works.

サインインしてコメントする。

採用された回答

Peter Perkins
Peter Perkins 2015 年 7 月 26 日
There's a function called standardizeMissing that would replace a non-NaN value with NaN, but normally, replacing NaN with a constant value (as opposed to, for example, some sort estimated value) would be kind of a funny thing to do. I'll assume you have a good reason.
Either of the following should work:
>> t = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'})
t =
Name Age Height Weight
_______ ___ ______ ______
'smith' 20 NaN 120
'jones' NaN 72 130
'doe' 40 66 140
>> vars = {'Age' 'Height'};
>> t2 = t{:,vars};
>> t2(isnan(t2)) = 0;
>> t{:,vars} = t2
t =
Name Age Height Weight
_______ ___ ______ ______
'smith' 20 0 120
'jones' 0 72 130
'doe' 40 66 140
>> t = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'});
>> [~,vars] = ismember({'Age' 'Height'},t.Properties.VariableNames)
vars =
2 3
>> for i=vars, t.(i)(isnan(t.(i))) = 0; end
Hope this helps.

その他の回答 (9 件)

Yuting Mou
Yuting Mou 2016 年 7 月 29 日
I also run across the problem, but there seems to be an easier way:
x.age(isnan(x.age)) = 0;
This is OK in my case
  1 件のコメント
Dooyoung Kim
Dooyoung Kim 2018 年 6 月 25 日
This works for me too! Thanks for the suggestion.

サインインしてコメントする。


Akira Agata
Akira Agata 2017 年 2 月 17 日
If you have R2016a or later version, you can use ismissing function and make it much easier.
For example:
% Make a sample table 'T' and replace 'NaN' with 0
T = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'});
idx = ismissing(T(:,{'Age','Height'}));
T{:,{'Age','Height'}}(idx) = 0;

Steven Lord
Steven Lord 2018 年 7 月 23 日
I would use the fillmissing function introduced in release R2016b. See the "Table with Multiple Data Types" example on that documentation page for a demonstration of how to replace NaN values with 0.
  1 件のコメント
Nurullah
Nurullah 2023 年 8 月 7 日
Thank you,

サインインしてコメントする。


J.M. Verduijn
J.M. Verduijn 2019 年 2 月 8 日
for i= 1: width(T)
T.(i)(isnan(T.(i))) = 0;
end
Works for me, replaces all NaN values in table T with 0
  1 件のコメント
Lautaro Parada
Lautaro Parada 2019 年 7 月 2 日
This is so simple and accurate! thanks!

サインインしてコメントする。


Zachary Smith
Zachary Smith 2020 年 3 月 19 日
If you are using readtable() to load the table from a file, then you can add the name-value pair argument 'EmptyValue',0 to do this automatically.

Aron Magesh
Aron Magesh 2021 年 3 月 7 日
編集済み: Aron Magesh 2021 年 3 月 7 日
Just use fillmissing function if the data is in a table or timetable.

Andy
Andy 2018 年 7 月 23 日
so, in other words:
mainTTable{:,:}(ismissing(mainTTable)) = 0;

carolina franco
carolina franco 2020 年 1 月 28 日
Hi,
Another simple way to understand what's going on .
For me, it works well in R2014a. You only need to enter the matrix with NaN values without specifying the columns where NaN values are.
%Input
m_data=C{1,1}; % Matrix with NaN values
%Code
s1=size(m_data,1);
for i= 1: s1
msubs=m_data(i,1:end); % Save existing data in ith row of m_data
msubs=msubs(isnan(m_data(i,1:end))==0); %Substitute matrix/ taking only non-NaN values
m_data(i,1:end)=0; %Erase all existing values in ith row of m_data
m_data(i,1:size(msubs,2))=msubs; %Substitute values without NaN
end
  2 件のコメント
Stephen23
Stephen23 2020 年 1 月 28 日
Note that all the original question explicitly states that "..I am using a table, not a matrix", and all of the other answers work with tables, not numeric matrices. Tables are a container array type:
carolina franco
carolina franco 2020 年 1 月 28 日
Right! Thanks

サインインしてコメントする。


Gabor
Gabor 2021 年 3 月 11 日
T{:,2:4}(ismissing(T{:,2:4})) = 0;
2:4 are the columns which are containing NaN values.
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 11 日
Interesting, that does work.
T = table({'smith';'jones';'doe'},[20;NaN;40],[NaN;72;66],[120;130;140],'VariableNames',{'Name' 'Age' 'Height' 'Weight'})
T = 3x4 table
Name Age Height Weight _________ ___ ______ ______ {'smith'} 20 NaN 120 {'jones'} NaN 72 130 {'doe' } 40 66 140
T{:,2:4}(ismissing(T{:,2:4})) = 0
T = 3x4 table
Name Age Height Weight _________ ___ ______ ______ {'smith'} 20 0 120 {'jones'} 0 72 130 {'doe' } 40 66 140

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by