Finding the percentage of NaN cells in columns
Ältere Kommentare anzeigen
I have an excel file. I want to know how much (%) of my data are NaN.
I want to found that for columns 9 to 12 separately, and have the results something like this:

I attached my excel file. this is just a sample and I have more than 125 excel files which I gonna do this for all. any help is appreciated.
thank you all
Akzeptierte Antwort
Weitere Antworten (2)
Jakob B. Nielsen
am 15 Jan. 2020
Lets say you have a vector A. isnan(A) gives you a vector of same dimension of A, with 0 on all indexes with a value, and 1 in all indexes with NaN. Therefore,
sum(isnan(A));
gives you the amount of NaN entries. And
(sum(isnan(A))/numel(A))*100;
gives the percentage.
Now, for doing it "easily" with excel, here is what you do. Put all excel files in the same folder, and then implement this. (This is a present from me to you, please look it through and understand it if you want to learn how to use it for other stuff :) )
[Name,Path]=uigetfile({'*.xls*'},'MultiSelect','on');
entirefile =fullfile(Path,Name);
filecount=size(Name,2);
% Now do a for loop to read all the selected files.
for i=1:filecount
Excel = actxserver('Excel.Application');
Excel.visible = false;
Excel.DisplayAlerts = false;
Excel.EnableSound = false;
Excel.Workbooks.Open(entirefile);
Workbook = Excel.ActiveWorkbook;
sheetIndex=1;
Worksheets.Item(sheetIndex).Activate;
Datainfo = get(Excel.ActiveSheet, 'Range', 'I2', 'L361'); %this is the range in the example file you attached... See comment below***
Data(j).yourdata=cell2mat(Datainfo.value);
Excel.Quit;
Excel.delete;
clear Excel;
end
You will end up with a data structure where, if you have 125 files, the Data structure will contain 125 entries of 360x4.
Data(1).yourdata will display all data from the first excel file, corresponding to Name{1}, and so on.
Then, you simply run the isnan function on all your data. You can even do that inside the above loop if you want.
*** IF your various excel files are not all of the same dimension, it gets a little hairy. I refer you to this post, which gives a code for finding the next empty row. Save the script as a sub function, and then call it this way:
lastrow = GoToNextRowInColumn(Excel,'A'); %DONT call it with column I or some such, because if your very last entry is a NaN you will skip it!
%%% and then change your Datainfo line above to this:
endrange=strcat('L',num2str(lastrow-1));
Datainfo = get(Excel.ActiveSheet, 'Range', 'I2', endrange);
1 Kommentar
BN
am 15 Jan. 2020
Using the C.mat file from your previous questions, here's a simple way to produce the table you're describing.
load('C.mat');
% Identify columns to analyze
col = [9 10 11 12];
% Compute percent-nan for each table & chosen column
nanPercent = cell2mat(cellfun(@(x)mean(isnan(x{:,col}),1),C,'UniformOutput',false)');
% If you want those values to be percentages rather than decimals (ie, 10.5 instead of 0.105)
% nanPercent = nanPercent * 100;
% Get the station_name from each table
stationNames = cellfun(@(x)unique(x.station_name(~ismissing(x.station_name))),C)';
% Summarize results in table
varNames = C{1}.Properties.VariableNames(col); % Get var names from first table
T = [table(stationNames), array2table(nanPercent,'VariableNames',varNames)];
Result
head(T) %show first few rows of table
stationNames tmax_m tmin_m rrr24 tm_m
__________________ ________ ________ ________ ________
{'Abadan' } 0.098684 0.13377 0.14254 0.12719
{'Abadeh' } 0.10965 0.12939 0.125 0.19079
{'Abali' } 0.14474 0.14474 0.17544 0.17982
{'Abumusa Island'} 0.16886 0.17105 0.13816 0.19079
{'Ahar' } 0.23246 0.22368 0.23465 0.26096
{'Ahvaz' } 0.059211 0.065789 0.065789 0.063596
{'Aligudarz' } 0.2193 0.24561 0.25219 0.25877
{'Anar' } 0.24342 0.21053 0.21491 0.25658
Note that percentages in this answer and Star Strider's answer differ only because we're using different inputs (apparently the C.mat files we're using are not the same file). When I test his code using my C.mat file, the outputs are the same.
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!