Replace NaN's in table with zero
조회 수: 357 (최근 30일)
이전 댓글 표시
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
채택된 답변
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.
댓글 수: 1
Chris Hooper
2024년 8월 23일
NaN does not work as desired in unique function:
An option in the unique fuction to treat NaN as a distinct would negate one reason for replacing NaN.
추가 답변 (9개)
Akira Agata
2017년 2월 17일
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;
댓글 수: 0
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
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.
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
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.
댓글 수: 0
Aron Magesh
2021년 3월 7일
편집: Aron Magesh
2021년 3월 7일
Just use fillmissing function if the data is in a table or timetable.
댓글 수: 1
Walter Roberson
2021년 3월 7일
That is what Steven Lord suggested in https://www.mathworks.com/matlabcentral/answers/231096-replace-nan-s-in-table-with-zero#answer_329967
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
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:
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
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{:,2:4}(ismissing(T{:,2:4})) = 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!