データセットやテーブルはどのように転置しますか?

24 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 16 Août 2018
データセットやテーブルのデータを転置したいのですが、どのようにすればよいでしょうか?
例えば以下のようなデータセットがあるとします。
>> X = dataset({[1;2], 'a'}, {[100;200], 'b'}, 'ObsNames', {'c','d'})
X =
a b
c 1 100
d 2 200
これを以下のようにする方法です。
>> Xt = X'
Xt =
c d
a 1 2
b 100 200
同様に、以下のようなテーブルを
>> X = array2table([1 100; 2 200],'VariableNames',{'a','b'},'RowNames',{'c','d'})
X =
a b
_ ___
c 1 100
d 2 200
以下のようにする方法です。
>> Xt = X'
Xt =
c d
___ ___
a 1 2
b 100 200

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 16 Août 2018
データセットやテーブルは転置することはできず、セルを使用したプログラミングで対処します。
データセットの場合はdataset2cellやcell2datasetを使用します。
>> Xc = dataset2cell(X)
Xc =
'ObsNames' 'a' 'b'
'c' [1] [100]
'd' [2] [200]
>> Xt = cell2dataset(Xc','ReadObsNames',true)
Xt =
c d
a 1 2
b 100 200
テーブルの場合はtable2cellやcell2tableを使用します。
>> Xc = table2cell(X)
Xc =
[1] [100]
[2] [200]
>> Xt = cell2table(Xc','RowNames',X.Properties.VariableNames,'VariableNames',X.Properties.RowNames)
Xt =
c d
___ ___
a 1 2
b 100 200
なお、R2018aからはrows2varsコマンドが提供されています。
<https://www.mathworks.com/help/matlab/ref/rows2vars.html>

Plus de réponses (0)

Catégories

En savoir plus sur table dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!