table is empty when being referenced Hi all!
I tried to select a column in a generated data table and transform that column into a new data table. Following is my code:
//part 1: generate a data table<BR />ret = New Table( title,
Add Rows (rowNum),
New Column( "P", Expression, "Continuous", Formula( pmatrix ) ),
New Column( "X(n)",
Expression,
"None",
Formula( If( Row() == 1, init_state, Lag( :Name( "X(n)" ), 1 ) * :p</img> ) ),
Set Selected,
),
New Column( "steady state",
Expression,
"None",
Formula( :Name( "X(n)" ) - Lag( :Name( "X(n)" ) ) == zeros ),
),
New Column( "States", Character, Values( names ) ),
);
// part 2: select column X(n) and transform it into a new table
<STRONG>mat = Column(ret,"X(n)")[1];
show(mat);</STRONG>
For(i = 2, i <= N Rows(ret), i++,
V Concat To(mat,Column(ret,"X(n)")[i]);
);
table = As Table(mat);
names = Column(ret, "States") << get values;
For(i = 1, i <= N Col(table), i++,
Column(i) << Set Name( names[i] );
);
When I tested the two parts seperately by saving the result table in part 1 and then running part 2, it works. But I don't want to save the table in part 1 everytime before running part 2 so I put them together. However, the merged script doesn't work. In this case, the table returned by part 2 only has missing values (Empty()).
I add "show(mat);" in the script to see what happened and it shows "mat = Empty();" in the log. The expected value of mat should be a vector as in each row of Column X(n) of the data table in part 1.
I don't know what went wrong. Any help would be appreciated!
Re: table is empty when being referencedThank you for the reply!Re: table is empty when being referencedAnd thanks for the report; the column function will do the <
Re: table is empty when being referencedwait(n) is not the best solution; it is hard to know if n is big enough. JMP evaluates the table formulas in the background. The best way to make sure they have evaluated is
ret << RunFormulas;
after building the table. It will evaluate the table's formulas faster than waiting for them.
Re: table is empty when being referencedI found a solution that can solve my problem: adding "wait(1)" between the two parts will work. (reference:
https://community.jmp.com/t5/Discussions/Add-new-column-with-formula-to-data-table-at-certain-position/td-p/14602).
but I don't understand why this works... Is there any other way that can also address this problem?
Re: table is empty when being referencedSorry. Don't know why something went wrong with the format. The first line of the script is commented...
ret = New Table(
Add Rows (rownum),
New Column( "P", Expression, "Continuous", Formula( ptable << get as matrix ) ),
New Column( "X(n)",
Expression,
"None",
Formula( If( Row() == 1, init_state, Lag( :Name( "X(n)" ), 1 ) * :p</img> ) ),
Set Selected,
),
New Column( "steady state",
Expression,
"None",
Formula( :Name( "X(n)" ) - Lag( :Name( "X(n)" ) ) == zeros ),
),
New Column( "States", Character, Values( names ) ),
);
Re: table is empty when being referencedI do not see the reference to the variable "ret". In the second half of the code, ret is used, but not in the 1st half......