get rid of series that contain useless values
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Adam Danz
2021 年 12 月 9 日
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.
採用された回答
Adam Danz
2021 年 12 月 9 日
編集済み: Adam Danz
2021 年 12 月 15 日
- 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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!