Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Highlighted
msharp
Super User

Stacking Just One Column

I was disappointed to find out that JMP resricts us from stacking just one column.  I understand that stacking just one column doesn't make sense, however, since it's just a data transform it does make sense to want to do it.  

 

Anyways, I wanted to open up the conversation and see who can come up with a better work around than I did.  Here's mine:

 

Names Default to Here(1);

//Example Data Table
dt = Open( "$SAMPLE_DATA/Decathlon.jmp" );

//col list is list of columns from 1 to n (never an empty list)
colList = {"Javelin", "Pole Vault"};

//Stack columns to prepare data for analysis
if(nitems(colList) > 1,
	analysisColsExpr = Expr(columns());
	for(i=1; imax=nitems(colList), i<=imax, i++,
		col = Column(dt, colList[i]);
		insert into(analysisColsExpr, col);
	);
	dtAnalysis = Eval(Eval Expr(dt << Stack(
		Expr( Name Expr( analysisColsExpr )),
		Source Label Column( "AnalysisType" ),
		Stacked Data Column( "AnalysisValue" ),
		Output Table( "Stacked Data" )
	)));
	close(dt, nosave);
,
	//Can't stack only one column adjust columns to match above strategy
	dt << New Column("AnalysisType", Character, Nominal, Formula(colList[1]));
	column(dt, colList[1]) << Set Name("AnalysisValue");
	dt << Set Name("Stacked Data");
	dtAnalysis = dt;
);

//Run Analysis
oneway = dtAnalysis << Oneway( Y( :AnalysisValue ), X( :AnalysisType ) );

Good Luck!

3 REPLIES 3
Highlighted
ms
Super User ms
Super User

Re: Stacking Just One Column

Here's an alternative that does not mess up the original table if there is only one column, i.e. creates a new table (like Stack() does) instead of adapting the original table to the approach.

Names Default To Here(1);

//Example Data Table
dt = Open("$SAMPLE_DATA/Decathlon.jmp");

//col list is list of columns from 1 to n (never an empty list)
colList = {"Javelin", "Pole Vault"};

If(N Items(colList) > 1,
    dtAnalysis = dt << Stack(
        Columns(colList),
        Source Label Column("AnalysisType"),
        Stacked Data Column("AnalysisValue"),
        Output Table("Stacked Data")
    ),
    dtAnalysis = New Table("Stacked Data",
        addrows(N Rows(dt)),
        New Column("AnalysisType", character, set each value(colList[1])),
        New Column("AnalysisValue", numeric, values(Column(dt, colList[1]) << getvalues))
    )
);

//Run Analysis
oneway = dtAnalysis << Oneway(Y(:AnalysisValue), X(:AnalysisType));
Highlighted
ms
Super User ms
Super User

Re: Stacking Just One Column

Another approach without a conditional (assuming original data table has >1 rows):

Names Default To Here(1);

//Example Data Table
dt = Open("$SAMPLE_DATA/Decathlon.jmp");

//col list is list of columns from 1 to n (never an empty list)
colList = {"Javelin", "Pole Vault"};
dtTemp = dt << Transpose(Private, Columns(colList), Label Column Name("AnalysisType"));
stackList = (dtTemp << get column names)[2 :: N Row(dt) + 1];
dtAnalysis = dtTemp << Stack(
    Columns(stackList),
    Stacked Data Column("AnalysisValue"),
    Output Table("Stacked Data")
);

//Run Analysis
oneway = dtAnalysis << Oneway(Y(:AnalysisValue), X(:AnalysisType));

 

Highlighted
msharp
Super User

Re: Stacking Just One Column

This is a great strategy and works perfectly in JMP 13, however, in JMP 12 since Name is a Label column it automatically throws it in as a label column for the Transpose function--short circuiting the strategy.  Not sure if that was a bug, but glad it is fixed in JMP 13.  Unfortunately, I'm the only one on my team that has JMP 13 currently...

Article Labels

    There are no labels assigned to this post.