cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
oto
oto
Level II

Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

Hi All,

I'm trying to generate a new column to be a mean column of :A by groups specified by :B, :C, :D.

Is there a way to do this where :B, :C, is a list? I'd like this so I can populate it using Col Dialog.

If not, is there a way to break the list into arguments?

 

The first line below gives me the desired means while the second line gives me the overall mean of :A.

mean= New Column("Mean", Formula(Col Mean(:A, :B, :C, :D)));
mean = New Column("Mean", Formula(Col Mean(:A, {:B, :C, :D})));

 

Thanks in Advance.

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

If you columns in a list with :name format, you can also use this option (idea from Using list of columns in formulas )

Names Default To Here(1);
// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open("$SAMPLE_DATA/big class.jmp");

col_list = {:height, :Sex, :Age};
formula_expr = Name Expr(Substitute(col_list, Expr(List()), Expr(Col Mean())));

Eval(EvalExpr(
	dt << New Column("Mean", Formula(Expr(formula_expr)));
));

For using Eval, Eval Expr, Expr, Substitute... see Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute 

-Jarmo

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

Here is an easy way to solve this

Names Default To Here( 1 );
// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open( "$SAMPLE_DATA/big class.jmp" );

colList = {"height", "Sex", "Age"};

Eval(
	Substitute(
			Expr(
				dt << New Column( "Mean",
					formula( Col Mean( __one__, __two__, __three__ ) )
				)
			),
		Expr( __one__ ), Parse( ":" || colList[1] ),
		Expr( __two__ ), Parse( ":" || colList[2] ),
		Expr( __three__ ), Parse( ":" || colList[3] )
	)
);
Jim
oto
oto
Level II

Re: Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

Thanks Jim, would there be a way to do this if the size of the list was undetermined?

txnelson
Super User

Re: Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

Of course there is.  @jthi shows one way of handling it, by converting the elements of the list you will be getting from your dialog box, to expressions.  Or, since JSL is a programming language, it provides you with all of the structures you need to solve the problem.  Given that you are somewhat new to JSL I strongly suggest that you take a look at the Scripting Guide.  It is the best way to learn about the structures and features of JSL.  The Scripting Guide is available in the JMP Documenation Library under the Help pull down menu.

 

Here is another way to handle the issue, which should give you what you want

Names Default To Here( 1 );
// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open( "$SAMPLE_DATA/big class.jmp" );

colList = {"height", "Sex", "Age"};

__vals__ = Concat Items( colList, "," );

Eval(
	Parse(
		Eval Insert(
			"dt << New Column( \!"Mean\!",
					formula( Col Mean( ^__vals__^) )
				)
			;"
		)
	)
);
Jim
jthi
Super User

Re: Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

If you columns in a list with :name format, you can also use this option (idea from Using list of columns in formulas )

Names Default To Here(1);
// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open("$SAMPLE_DATA/big class.jmp");

col_list = {:height, :Sex, :Age};
formula_expr = Name Expr(Substitute(col_list, Expr(List()), Expr(Col Mean())));

Eval(EvalExpr(
	dt << New Column("Mean", Formula(Expr(formula_expr)));
));

For using Eval, Eval Expr, Expr, Substitute... see Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute 

-Jarmo
oto
oto
Level II

Re: Is there a a way to input a list of columns as by group variables into column mean/std dev/median?

Thank you all, this got me down the right direction.

Still learning about expressions and evaluations, thank you for the example.