Replace NaN values with values based on same group and other group?

2 ビュー (過去 30 日間)
Kyle Campbell
Kyle Campbell 2016 年 9 月 12 日
コメント済み: Kyle Campbell 2016 年 9 月 13 日
  • table (650,000x20)
  • customerID group
  • cost group (NaN values throughout)
The table contains 5-7 data lines for each customerID that have the same cost except for one or two NaN cost values. It is obvious to replace the NaN values with the same other given cost values associated with the ID. But I am having trouble.
I was thinking:
Table.cost(isnan(Table.cost)) = Table.customerID(mode(Train.cost));
But this would apply to the entire rather than correct customerID?

採用された回答

Teja Muppirala
Teja Muppirala 2016 年 9 月 13 日
I think this should work.
% Sample data
T = cell2table({'ID1' 100 'aaa';
'ID1' nan 'bbb';
'ID1' nan 'ccc';
'ID2' 200 'ddd';
'ID2' nan 'eee';},'VariableNames',{'customerID','cost','otherStuff'})
% Replace the NaNs with the mode of that group
G = findgroups(T.customerID);
val = splitapply(@(x){mode(x)*ones(size(x))},T.cost,G);
T.cost = cat(1,val{:}) % <----- Fixed now
  1 件のコメント
Kyle Campbell
Kyle Campbell 2016 年 9 月 13 日
This worked beautifully thank you!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 9 月 13 日

カテゴリ

Find more on Matrices and Arrays in Help Center and File Exchange

製品

Community Treasure Hunt

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

Start Hunting!

Translated by