get rid of series that contain useless values
1 view (last 30 days)
Show older comments
Christos Papagrigoriou
on 9 Dec 2021
Hello,
I have imported a table in a script and I want to create a loop that deletes all the series in the table that contain cells with 'Not collected' and 'Unknown Values'. The code that currently use is
data0= readtable('NSCLCR01Radiogenomic_DATA_LABEL.csv');
data1=data0(:,[3,5,7,18,25,26,27,28,29,30,31,32]);
data1(49,:) = [];
for i = 1: width(data1)
for j = 1:12
s1 = data1(i,j);
s2 = 'Not collected';
s3 = 'Unknown';
tf = strcmp(s1,s2);
tf2 = strcmp(s1,s3) ;
if tf == 1 || tf2 == 1 ;
else
newdata(i,j) = data1(i,j)
end
end
end
newdata(~cellfun('isempty',R))
but it does not seem to gimme back the desirable results.
cheersxxx
2 Comments
Adam Danz
on 9 Dec 2021
Do you want to delete rows or columns that contain those key words?
BTW, the file you uploaded is xlsx (to csv) and contains only 17 columns but your code is looking for up to 32 columns.
Accepted Answer
Adam Danz
on 9 Dec 2021
Edited: Adam Danz
on 15 Dec 2021
- Load the data
- use varfun to determine which columns of the table are cell-strings or strings
- use ismember find a list of key words ("not collected", "unknown", etc). This code ignores case.
- Use indexing and any to eliminate any row that contains a key word.
data0= readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/828830/NSCLCR01Radiogenomic_DATA_LABEL.xlsx');
isstr = varfun(@(c)iscellstr(c)||isstring(c), data0,'OutputFormat','uniform');
nullKeys = {'Not collected', 'Unknown'}; % not case sensitive; add more as needed
dataStr = data0{:, isstr};
isnull = ismember(lower(dataStr), lower(nullKeys));
% Remove rows of table that contains a null indicator
rowContainsNull = any(isnull,2);
data0(rowContainsNull, :) = []
Or, if you want to remove columns with key words,
% Remove cols of table that contains a null indicator
colContainsNull = any(isnull,1);
tblColIdx = ismember(cumsum(isstr) .* isstr, find(colContainsNull));
data0(:, tblColIdx) = []
0 Comments
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!