How can I re-code numeric variables in a table to strings?
Show older comments
I am using Matlab version 9.1.0.441655 (R2016).
I have created a large (2496000x8) table using array2table. Although it was simpler to create the initial matrix as uniformly numeric, for analysis I would like re-code the numbers in several variables to be strings, while leaving other variable as numeric.
An example data set:
Var1=[1; 2; 1; 2; 2; 1; 1];
Var2=[2; 2; 1; 1; 1; 2; 1];
Var3=[32.1; 34.6; 56.7; 65.3; 78.6; 23.4; 43.1];
Mat=[Var1,Var2,Var3];
Table=array2table(Mat,'VariableNames',{'Var1','Var2','Var3'});
Table =
Var1 Var2 Var3
____ ____ ____
1 2 32.1
2 2 34.6
1 1 56.7
2 1 65.3
2 1 78.6
1 2 23.4
1 1 43.1
I would like to re-code the 1's and 2's in Var1 to 'Male' and 'Female', respectively and re-code the 1's and 2's in Var2 to 'Old' and 'Young', respectively. But, leave Var3 as numeric.
To result in:
Var1 Var2 Var3
________ _______ _________
'Male' 'Young' [32.1000]
'Female' 'Young' [34.6000]
'Male' 'Old' [56.7000]
'Female' 'Old' [65.3000]
'Female' 'Old' [78.6000]
'Male' 'Old' [23.4000]
'Male' 'Young' [43.1000]
I tried:
Table.Var1(strcmp(Table.Var1,1)) = {'Male'}
But that gives a conversion error:
Conversion to double from cell is not possible.
Accepted Answer
More Answers (2)
Andrei Bobrov
on 6 Sep 2017
Edited: Andrei Bobrov
on 7 Sep 2017
c = {'Male','Female','Old','Young'};
T_out = cell2table(c(cell2mat(Table{:,:})+[0 1]));
or for the latest versions of the MATLAB:
Table(:,1:2) = array2table(categorical(Table{:,1:2}+[0 1],1:4,{'Male','Female','Old','Young'}));
or with new conditions
Var1=[1; 2; 1; 2; 2; 1; 1];
Var2=[2; 2; 1; 1; 1; 2; 1];
Var3=[32.1; 34.6; 56.7; 65.3; 78.6; 23.4; 43.1];
Table=table(Var1,Var2,Var3);
c = {'Male','Female','Old','Young'};
T_out = [cell2table(c(bsxfun(@plus,Table{:,1:2},0:1))),Table(:,3)];
2 Comments
JLC
on 6 Sep 2017
Andrei Bobrov
on 6 Sep 2017
Please see third part "with new conditions" of my answer.
Image Analyst
on 6 Sep 2017
Try this:
Var1={1,2,1,2,2,1,1};
Var2={2,2,1,1,1,2,1};
Mat=[Var1',Var2'];
Table=array2table(Mat,'VariableNames',{'Var1','Var2'})
% Find where column 1 = 1.
rowsWith1 = cell2mat(Table.Var1) == 1
% Replace those rows with 'Male' and other rows with 'Female'.
Table(rowsWith1, 'Var1') = {'Male'}
Table(~rowsWith1, 'Var1') = {'Female'}
% Find where column 1 = 1.
rowsWith1 = cell2mat(Table.Var2) == 1
% Replace those rows with 'Young' and other rows with 'Old'.
Table(rowsWith1, 'Var2') = {'Young'}
Table(~rowsWith1, 'Var2') = {'Old'}
2 Comments
Image Analyst
on 6 Sep 2017
JLC:
The problem is that with tables, all elements in a column must be of the same type. So if you started with numbers, you can't then set some elements in some rows to 'Male' which is a string because then the column would contain both numbers and strings, which is not allowed with tables. You CAN to that with cell arrays however. So I converted your table to a cell array, then made the replacements, and then (now that they're all strings) converted back to a table. See this:
Var1=[1; 2; 1; 2; 2; 1; 1];
Var2=[2; 2; 1; 1; 1; 2; 1];
Var3=[32.1; 34.6; 56.7; 65.3; 78.6; 23.4; 43.1];
Mat=[Var1,Var2,Var3];
Table=array2table(Mat,'VariableNames',{'Var1','Var2','Var3'})
% Need to create a cell array, because with a table we can't have some rows in the Var1 column
% be numbers while others are strings. However a cell array can do that.
ca = table2cell(Table)
% Find where column 1 = 1.
rowsWith1 = Table{:, 'Var1'} == 1
% Replace those rows with 'Male' and other rows with 'Female'.
ca(rowsWith1, 1) = {'Male'}
ca(~rowsWith1, 1) = {'Female'}
% Find where column 1 = 1.
rowsWith1 = Table{:, 'Var2'} == 1
% Replace those rows with 'Young' and other rows with 'Old'.
ca(rowsWith1, 2) = {'Young'}
ca(~rowsWith1, 2) = {'Old'}
% Now convert from cell array back to table.
Table = cell2table(ca, 'VariableNames',{'Var1','Var2','Var3'})
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!