私は、ケースごとに年数のデータ(体重など)が奇数の垂直データセットを持っています。 1、2、3、4、6 年のものもあれば、3、4、5 年のものもあります。 私は、各ケースに 1,2,3,4,5,6 を含む年列を含む 6 行を持たせたいと考えていますが、これらの年には体重がないものもあります。 欠落している適切な年を認識する各ケースに 6 行を割り当てるにはどうすればよいですか?
大量のデータがあると仮定すると、単純なスクリプトを使用して不足している年を追加できます。以下は、いくつかの欠落した年を含む元のテーブルを作成し、その時点からいくつかの手順を実行して、欠落した年が追加される例です。
names default to here(1);
dt = New Table( "Base",
Add Rows( 10 ),
New Column( "Case",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 1, 1, 1, 1, 2, 2, 2, 2, 2] ),
Set Display Width( 43 )
),
New Column( "Year",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 2, 3, 5, 6, 1, 2, 4, 5, 6] )
),
New Column( "OtherStuff", formula(randomuniform()))
);
// Summarize original data table to get a data table of the cases
dtCase = dt << Summary(
Group( :Case ),
Freq( "None" ),
Weight( "None" ),
output table name( "Cases" )
);
dtCase << delete columns(:N Rows);
// Create a new table with just the years
dtYears = new table("years", new column("Year", values([1,2,3,4,5,6])));
// Perform a cartesian join to create a table with all combinations of cases and years
dtCY = Data Table( "Cases" ) << Join(
With( Data Table( "years" ) ),
Cartesian Join,
Output Table( "Case and Years" )
);
// Use a join on the original table to add in the misssing years
dt << Join(
With( dtCY ),
Merge Same Name Columns,
Match Flag( 0 ),
By Matching Columns( :Case = :Case, :Year = :Year ),
Drop multiples( 0, 0 ),
Include Nonmatches( 0, 1 ),
Preserve main table order( 1 ),
Output Table( "Final" )
);
これはうまくいきます! ケースを取得するために元のテーブルに割り当てる必要がありましたが、全体的な方法は正常に機能します。 どうもありがとう!