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

6 Ansichten (letzte 30 Tage)
Robert am 12 Mär. 2018
Kommentiert: Robert am 12 Mär. 2018
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'.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Guillaume am 12 Mär. 2018
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 KommentareKeine anzeigenKeine ausblenden
Robert am 12 Mär. 2018
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 am 12 Mär. 2018
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
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).

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

KSSV am 12 Mär. 2018
Bearbeitet: KSSV am 12 Mär. 2018
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 KommentareKeine anzeigenKeine ausblenden
Robert am 12 Mär. 2018
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 am 12 Mär. 2018
Thank you so much KSSV, and sorry I was not clear the first time.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Managing Data finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by