Filling missing values of selected columns of a table with previous values
조회 수: 11 (최근 30일)
이전 댓글 표시
I am trying to fill the NaNs from a selected columns of interest with previous values.
For example lets say i have a table Data as:
SN English Science Maths Health
1 100 80 89 74
2 NaN 85 NaN 89
3 NaN NaN 75 NaN
4 80 NaN NaN NaN
5 NaN 75 NaN 60
Lets say if i only want to fill with previous values for column English and Maths. How do i do it? I tried this line. I went to error as
Data=fillmissing(Data{:,'English','Science'},'previous');
You cannot subscript a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts). Use a
row subscript and a variable subscript.
댓글 수: 0
답변 (3개)
Guillaume
2019년 4월 24일
Star's answer works (please don't delete it!) or you can use:
Data(:, {'English', 'Science'}) = fillmissing(Data(:, {'English', 'Science'}), 'previous');
You need to understand the difference between {} indexing (extracts content of a table as a cell array or matrix) and () indexing (extracts portion of the table, resut is still a table) for tables.
댓글 수: 0
Star Strider
2019년 4월 23일
You most likely have to use fillmissing with each variable separately:
Data.English = fillmissing(Data.English,'previous');
Data.Science = fillmissing(Data.Science,'previous');
That works.
댓글 수: 2
Star Strider
2019년 4월 24일
You can, but apparently only one at a time, as in my code. I experimented with varfun, using an anonymous function with fillmissing, however varfun threw an error about anonymous functions (that made no sense to me, since the anonymous function worked outside of varfun).
Sam17
2019년 4월 24일
댓글 수: 2
Guillaume
2019년 4월 24일
This is a different question from your original one, so please start a new question. It certainly shouldn't be an answer to the current question (for which you should accept an answer if one actually solved your original problem).
When you ask your new question, you will most likely be asked: Have you used the profiler to find which part of the code is slow?
Note that none of the find are needed in your code (but the impact is most likely very minimal)
index = ismember(variables,interpolate);
Data=Total_Data(:,index);
works just as well.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!