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

4 次查看(过去 30 天)
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by