How to covert rownames to the first variable in the table?

조회 수: 51 (최근 30일)
monkeyquant
monkeyquant 2023년 1월 25일
답변: Star Strider 2023년 1월 25일
I have a table with rownames (nuemric value) and want to convert it to a table with a newly added variable from the rownames.

채택된 답변

Star Strider
Star Strider 2023년 1월 25일
Try something like this —
T1 = array2table(randi(9, 5, 5));
T1.Properties.RowNames = {'1','2','3','4','5'} % Create Table With Row Names
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 1 2 8 8 8 3 2 1 3 8 8 7 3 5 8 5 5 7 4 9 3 1 2 1 5 2 5 1 7 2
RN = T1.Properties.RowNames % Get Row Names
RN = 5×1 cell array
{'1'} {'2'} {'3'} {'4'} {'5'}
T1 = addvars(T1, cell2mat(RN), 'Before','Var1') % Convert Row Names To First Variable
T1 = 5×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 ______ ____ ____ ____ ____ ____ 1 1 2 8 8 8 3 2 2 1 3 8 8 7 3 3 5 8 5 5 7 4 4 9 3 1 2 1 5 5 2 5 1 7 2
T1.Properties.RowNames = {} % Delete Original Row Names
T1 = 5×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 ______ ____ ____ ____ ____ ____ 1 2 8 8 8 3 2 1 3 8 8 7 3 5 8 5 5 7 4 9 3 1 2 1 5 2 5 1 7 2
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by