각 경우에 대해 연도가 고르지 않은 데이터(예: 체중)가 포함된 수직 데이터 세트가 있습니다. 일부는 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" )
);
작동합니다! 사례를 얻기 위해 원래 테이블에 할당해야 했지만 전반적인 방법은 잘 작동합니다. 고맙습니다!