How to replace numbers (-999) for NaN in a dataset?

4 ビュー (過去 30 日間)
Robert
Robert 2018 年 3 月 12 日
コメント済み: Robert 2018 年 3 月 12 日
Hi,
I have a large dataset with variables containing -999. I want to change those numbers to NaN but it is not working...
I2=find(Dataset==-999); Dataset(I2)=NaN;
...this is the error I get:
Undefined operator '==' for input arguments of type 'dataset'.
Thank you for your help!

採用された回答

Guillaume
Guillaume 2018 年 3 月 12 日
Note: datasets have been deprecated for a while. Consider using tables instead.
A dataset is a collection of variables. So you have to do your replacement per variable. e.g.
yourdataset.somevariable(yourdataset.somevariable == -999) = nan;
%same for all variables
Another option would be to use replacedata. For it to work you'll have to create an m file for a replacement function:
function X = datasetreplace(X)
X(X == -999) = nan;
end
You can then do:
newdataset = replacedata(yourdataset, @datasetreplace)
  2 件のコメント
Robert
Robert 2018 年 3 月 12 日
Thank you so much for your help! I also appreciate your comment on dataset vs table, and to give me two options to proceed with my analysis.
Robert
Robert 2018 年 3 月 12 日
Hi Guillaume,
Thank you for your help. I am wondering if I could follow with some questions on table? Or should I open another question? as this is a follow up to previous question...
It is OK with you, here is my question. I am trying to use the function grpstats as following:
cd
load Example4Guillaume.mat
Matrix1.Data(Matrix1.Data == -999)=nan;
%creating submatrix and changing variables to nominal
dsa1 = Matrix1(:,{'Location','Depth','Event','Data'});
dsa1.Location = nominal(dsa1.Location);
dsa1.Depth = nominal(dsa1.Depth);
dsa1.Event = nominal(dsa1.Event);
%Applying statarray
statarray = grpstats(dsa1,'Depth');
I am trying to follow ‘hospital’ (https://www.mathworks.com/help/stats/grpstats.html) example but getting the following error:
Error using dsgrpstats (line 266)
Data variables must be numeric or logical.
Error in grpstats (line 136)
[varargout{1:nargout}] =
dsgrpstats(x,group,whichstats,varargin{:});
Not sure what I am doing wrong, any advice is welcome!
Attached is a very small example of my very large matrix (Example4Guillaume.mat).

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

その他の回答 (1 件)

KSSV
KSSV 2018 年 3 月 12 日
編集済み: KSSV 2018 年 3 月 12 日
A = [-999 3 4 -999 4 5 -999] ;
B = A ;
B(B==-999) = NaN ;
Don't use find. It will be slow..use logical indexing..it will be quite fast.
  2 件のコメント
Robert
Robert 2018 年 3 月 12 日
Sorry, I was not clear, I need to do that to a 'dataset array'. That is because I get the error: Undefined operator '==' for input arguments of type 'dataset'.
Robert
Robert 2018 年 3 月 12 日
Thank you so much KSSV, and sorry I was not clear the first time.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by