get rid of series that contain useless values
조회 수: 2 (최근 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에서 Tables에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!