cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
Pim
Pim
Level II

Fill data table columns with values from another data table

Hi JMP community,

 

I have a question regarding 2 data tables for which one of the data tables should have rows filled with values of columns from the other data table.

 

Here an example:

I have a data table (Table 1) which contains 2 columns that both have one value. In a second data table (Table 2) I have 2 columns with 9 values each. What I want is to copy the Result 1 and Result 2 column values of Table 1 to all 9 rows of Table 2. Can anyone help me correct this script? I'm using JMP 16.

Names Default To Here( 1 );

dt1 = New Table( "Table 1", 
	Add Rows( 1 ), 
	New Column( "Result 1", Numeric, Continuous, Set values( [3] ) ), 
	New Column( "Result 2", Numeric, Continuous, Set values( [5] ) ), 
);

val1 = dt1:"Result 1"n << Get values;
val2 = dt1:"Result 2"n << Get values;

dt2 = New Table( "Table 2" );

dt2 << New Column( "Test1", Numeric, Continuous, Set values( [1, 1, 1, 1, 1, 1, 1, 1, 1] ) );
dt2 << New Column( "Test2", Numeric, Continuous, Set values( [2, 2, 2, 2, 2, 2, 2, 2, 2] ) );
dt2 << New Column( "Result 1", Numeric, Continuous, <<Set each value( val1 ) );
dt2 << New Column( "Result 2", Numeric, Continuous, <<Set each value( val2 ) );

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Fill data table columns with values from another data table

Hi,

 

I believe the reason the script above doesn't work is because the "<<Set each value" command requires a number as input, rather than a matrix; val1 and val2 are matrices.

 

One way to solve this is to change the last two lines accordingly:

dt2<<New Column( "Result 1", Numeric, Continuous,<<Set each value(val1[1]));

dt2<<New Column( "Result 2", Numeric, Continuous,<<Set each value(val2[1]));

Another way is to use "Set Values" by converting val1 and val2 into 9x1 matrices:

dt2<<New Column( "Result 1", Numeric, Continuous,<<Set values(J(nrows(dt2),1,val1)));

dt2<<New Column( "Result 2", Numeric, Continuous,<<Set values(J(nrows(dt2),1,val2)));

 

View solution in original post

2 REPLIES 2

Re: Fill data table columns with values from another data table

Hi,

 

I believe the reason the script above doesn't work is because the "<<Set each value" command requires a number as input, rather than a matrix; val1 and val2 are matrices.

 

One way to solve this is to change the last two lines accordingly:

dt2<<New Column( "Result 1", Numeric, Continuous,<<Set each value(val1[1]));

dt2<<New Column( "Result 2", Numeric, Continuous,<<Set each value(val2[1]));

Another way is to use "Set Values" by converting val1 and val2 into 9x1 matrices:

dt2<<New Column( "Result 1", Numeric, Continuous,<<Set values(J(nrows(dt2),1,val1)));

dt2<<New Column( "Result 2", Numeric, Continuous,<<Set values(J(nrows(dt2),1,val2)));

 

Pim
Pim
Level II

Re: Fill data table columns with values from another data table

Thank you Hadley!  Your solution works perfectly!