Converting multiple table variables from string to categorical data
조회 수: 7 (최근 30일)
이전 댓글 표시
I've figured out how to use the categorical function to convert a single table variable (i.e., a single column of table data) to categorical data using the table.variable=categorical(table.variable) method but is there not a way to convert multiple variables/columns from string to categorical with a single command?
Thanks,
JK
댓글 수: 0
답변 (1개)
Tim leonard
2015년 2월 12일
I'd love to know if this is possible as well. This is the best I can figure out, but still not really at all elegant.
%make table
tblIn= table({'cat';'dog';'bird'},{'bat';'rat';'sat'},(1:3)')
%which cols to convert
cellOfColumnNames = {'Var1','Var2'}
tblOut = [tblIn(:,~ismember(tblIn.Properties.VariableNames,cellOfColumnNames))...
varfun(@categorical,tblIn,'inputvariables',cellOfColumnNames)];
% or, unpacked a little bit:
newTable = varfun(@categorical,tblIn,'inputvariables',cellOfColumnNames);
tblOut = [tblIn(:,~ismember(tblIn.Properties.VariableNames,cellOfColumnNames))...
newTable];
댓글 수: 2
Dhruv Ghulati
2015년 12월 18일
This is a great answer. I tried to use the same thing for the function str2double, but I got an error
Error using table (line 253) All variables must have the same number of rows.
Error in table/varfun (line 220) b = table(b{:},'VariableNames',b_varnames);
continousVars = {'artists_paying_for', 'bookmarks','connected_ga','connected_fb',...
'connected_itunes','free_artists','subscribed_artists','usersincompany',...
'companyartistspaidfor','sincelastcontacted','sincelastheardfrom','sincefirstseen',...
'sincesignedup','sincecompanylastseen','sincecompletedsetup','sinceusercreated',...
'sinceaccountcreated','sincecompanycreated'};
newTable = varfun(@str2double,cleanNBS,'inputvariables',continousVars);
T = [cleanNBS(:,~ismember(cleanNBS.Properties.VariableNames,catVars))...
newTable];
Any ideas?
Guillaume
2015년 12월 18일
Please start your own question (you can link to this one to add context) rather than hijacking somebody's else.
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!