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

조회 수: 7 (최근 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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by