Ho un set di dati verticali con un numero dispari di dati sugli anni (peso corporeo, ad esempio) per ciascun caso. Alcuni hanno anni 1,2,3,4,6 e alcuni hanno 3,4,5. Voglio che ogni caso abbia 6 righe con la colonna dell'anno contenente 1,2,3,4,5,6 anche se alcuni di quegli anni non avranno un peso corporeo. Come posso assegnare ad ogni caso 6 righe che riconoscono l'appropriato anno mancante?
Supponendo di disporre di una buona quantità di dati, è possibile utilizzare un semplice script per aggiungere gli anni mancanti.Di seguito è riportato un esempio che crea una tabella originale con alcuni anni mancanti, quindi da quel punto esegue un paio di passaggi che finiscono con l'aggiunta degli anni mancanti
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" )
);
Questo funziona! Ho dovuto assegnare alla mia tabella originale per ottenere i casi, ma il metodo generale funziona bene. Grazie mille!