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
BladeRunner
Level II

Copying selected cells from one column and pasting them into selected cells in another column

Hello, I am a JSL beginner. I need to learn how to move a block of cells in one column to a different location in a different, newly created column.  In this case, on top of this new column.

 

Specifically, I need to select a set of cells in column THK marked as PRE in a separate PRE/POST column, and paste them on top of a new column marked THK PRE. It is somewhat similar to the Split Column function, but I do not want to use the Split Column function as I find that, due to some glitch, the data sometimes appears scrambled in the newly created column if the data in some cells of the THK column is missing.

 

From some examples on the forums, I put together the code below, but it does not paste any values in the newly created THK PRE column.  Please help!  Many thanks in advance.

 

Here's what I need:

 

Starting data table

 

THK     I    PRE/POST 

9.5            POST

9.8            POST

10.1          POST

11.0           PRE

11.4          PRE

10.9          PRE

 

Here's what I need the data table to look like after this operation:

 

THK     |     PRE/POST   |   THK PRE

9.5            POST                 11.0

9.8            POST                 11.4

10.1         POST                  10.9

11.0          PRE

11.4          PRE

10.9          PRE

 

The script below does not paste any values in the newly created column, the column remains empty

 

 

// Pick the source data table
dt=Open("Data Table");

//Create New THK PRE column
dt << New Column( "THK PRE", Numeric, "Continuous");
Wait(1);


// Select PRE values from THK column
vals = Column(dt, "THK") << Get Rows Where(:Name("PRE/POST") == "PRE");

// Put these in the new column THK PRE
Column(dt, "THK PRE") << Set Values(vals)

 

 

Please help! Many thanks in advance.  I use JUMP 16.0.0

 

@jthi 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Copying selected cells from one column and pasting them into selected cells in another column

Here is a rework of your script with some added annotation that handles what you want

Names Default To Here( 1 );

// Pick the source data table
//dt=Open("Data Table");
dt = New Table( "Example",
	Add Rows( 6 ),
	New Column( "THK",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [9.5, 9.8, 10.1, 11, 11.4, 10.9] )
	),
	New Column( "PRE/POST",
		Character,
		"Nominal",
		Set Values( {"POST", "POST", "POST", "PRE", "PRE", "PRE"} )
	)
);

//Create New THK PRE column
dt << New Column( "THK PRE", Numeric, "Continuous" );
Wait( 0 );


// Find the rows PRE values from THK column
// and copy those values from the found rows
// to the vals matrix 
vals = :THK[dt << get rows Where( :Name( "PRE/POST" ) == "PRE" )];

// Put these in the new column THK PRE
dt:THK PRE << Set Values( vals );
Jim

View solution in original post

6 REPLIES 6
txnelson
Super User

Re: Copying selected cells from one column and pasting them into selected cells in another column

Here is a rework of your script with some added annotation that handles what you want

Names Default To Here( 1 );

// Pick the source data table
//dt=Open("Data Table");
dt = New Table( "Example",
	Add Rows( 6 ),
	New Column( "THK",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [9.5, 9.8, 10.1, 11, 11.4, 10.9] )
	),
	New Column( "PRE/POST",
		Character,
		"Nominal",
		Set Values( {"POST", "POST", "POST", "PRE", "PRE", "PRE"} )
	)
);

//Create New THK PRE column
dt << New Column( "THK PRE", Numeric, "Continuous" );
Wait( 0 );


// Find the rows PRE values from THK column
// and copy those values from the found rows
// to the vals matrix 
vals = :THK[dt << get rows Where( :Name( "PRE/POST" ) == "PRE" )];

// Put these in the new column THK PRE
dt:THK PRE << Set Values( vals );
Jim
BladeRunner
Level II

Re: Copying selected cells from one column and pasting them into selected cells in another column

Hello Txnelson, many thanks for the quick response!  Works like a charm!   I guess I was pretty close, but couldn't quite figure our the syntax.  

steveylin
Level II

Re: Copying selected cells from one column and pasting them into selected cells in another column

is there a way to paste the selected data into a new column in the same row position? or set the values of a new column based on selection?  Something like this?  

steveylin_0-1721140991697.png

 

txnelson
Super User

Re: Copying selected cells from one column and pasting them into selected cells in another column

Here are a couple of ways to do what you want.  

Names Default To Here( 1 );

// Pick the source data table
//dt=Open("Data Table");
dt = New Table( "Example",
	Add Rows( 6 ),
	New Column( "THK",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [9.5, 9.8, 10.1, 11, 11.4, 10.9] )
	),
	New Column( "PRE/POST",
		Character,
		"Nominal",
		Set Values( {"POST", "POST", "POST", "PRE", "PRE", "PRE"} )
	)
);

//Create New THK PRE column
dt << New Column( "THK PRE1",
	Numeric,
	"Continuous",
	formula( If( :"PRE/POST"n == "PRE", :THK, . ) )
);

dt << New Column( "THK PRE2", Numeric, "Continuous" );

For Each Row(
	If( :"PRE/POST"n == "PRE",
		:THK PRE2 = :THK,
		:THK PRE2 = .
	)
);

If you want the THK PRE value to be a 5 as illustrated in your second example, just change the resulting value from :THK to 5

Jim
steveylin
Level II

Re: Copying selected cells from one column and pasting them into selected cells in another column

Appreciate the response.  I was hoping there was a 'paste' script function that I was not aware of.  Looks like I will have to loop through each row as you suggested if the column already contains other values.

hogi
Level XII

Re: Copying selected cells from one column and pasting them into selected cells in another column

A bit closer to copy/paste: you can get and batch write all values at once:
Data table subscripting 

 

Names Default To Here( 1 );

dt = New Table( "Example",
	Add Rows( 100 ),
	New Column( "input",
		Set each Value( random uniform () )
	),
	New Column( "variant",
		Character,
		set each value({"A","B"}[random integer(1,2)])
	)
);

dt << New Column( "out");
rows= Where(:variant == "A");
vals = dt[rows,"input"];
dt[rows,"out"]=vals;

 

This is similar to 

dt:THK PRE << Set Values( vals );

The difference: via data table indexing you can precisely specify into which rows to write. 

Set values starts at row 1 and fills as many rows as there are values in the array.