필터 지우기
필터 지우기

How to use fillmissing function within a group ?

조회 수: 7 (최근 30일)
Namrata Goswami
Namrata Goswami 2020년 12월 11일
댓글: Namrata Goswami 2020년 12월 14일
I have a table where multiple columns have NaN and I want the NaN to be replaced by the previous valid value in the column, within the group(by ID.
T =
ID amount1 amount2
15 200 300
15 NaN 800
15 NaN NaN
67 NaN 300
67 400 700
67 NaN NaN
Expected output:
ID amount1 amount2
15 200 300
15 200 800
15 200 800
67 NaN 300
67 400 700
67 400 700
I am able to achieve this partially using fillmissing function as:
Tupdated = fillmissing(T,'previous');
But I want to implement this within group (by ID). I am unable to use fillmissing with splitapply correctly.

채택된 답변

dpb
dpb 2020년 12월 11일
>> varfun(@(x) fillmissing(x,'previous'),T,'groupingvariables',{'ID'},'InputVariables',{'amount1','amount2'})
ans =
6×4 table
ID GroupCount Fun_amount1 Fun_amount2
__ __________ ___________ ___________
15 3 200 300
15 3 200 800
15 3 200 800
67 3 NaN 300
67 3 400 700
67 3 400 700
>>

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by