cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
j821005
Level II

How to assign a series of value to selected rows without using a for loop?

Hi, all
I used get rows where() to get a series of rows and want to assign the values in the rows under column b to the same rows under another column, like below

 

rows = dt << get rows where (column("b")[ ] == "xxx");
column ("a")[rows] = column ("b") [rows];


But it doesn't work, want to know is there any way to assign a series of values to selected rows without using a for loop?
Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: How to assign a series of value to selected rows without using a for loop?

Data table subscripting  might be what you are looking for. You can probably use something like dt[rows,{a}] = dt[rows,{b}]; where rows is a matrix with the row indexes and {a} and {b} are lists of columns. You'll also need the explicit dt which is returned by open() when you open the table, or by other functions like subset(), etc. that make new tables.

 

Craige

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: How to assign a series of value to selected rows without using a for loop?

Here is an example that shows one way to solve your issue

names default to here(1);
dt=open("$sample_data/big class.jmp");

// Create a new table with the same number of rows and a new
// character column called Gender
dtNew = New Table("New Table", add rows(nrows(dt)),
	New Column("Gender",character)
);

// Assign to all rows in big class that are females, the value
// of "Female" to the same rows in New Table
dtNew:Gender[dt<<get rows where(:sex=="F")]="Female";
Jim
j821005
Level II

Re: How to assign a series of value to selected rows without using a for loop?

Thanks for your reply.
But instead of assign "a certain value", I want to assign "a series of values" to corresponding rows without a for loop.
txnelson
Super User

Re: How to assign a series of value to selected rows without using a for loop?

Others may have a more efficient way of doing this, but here is my solution

names default to here(1);
dt=open("$sample_data/big class.jmp");

// Create a new table with the same number of rows and a new
// character column called Gender
dtNew = New Table("New Table", add rows(nrows(dt)),
	New Column("Gender",character)
);

// Assign to all rows in big class that are females, the value
// of "Female" to the same rows in New Table
values = dt:sex << get values;
dtNew:Gender << set values(values);
dtNew:Gender[dt<<get rows where(:sex!="F")]="";
Jim
Craige_Hales
Super User

Re: How to assign a series of value to selected rows without using a for loop?

Data table subscripting  might be what you are looking for. You can probably use something like dt[rows,{a}] = dt[rows,{b}]; where rows is a matrix with the row indexes and {a} and {b} are lists of columns. You'll also need the explicit dt which is returned by open() when you open the table, or by other functions like subset(), etc. that make new tables.

 

Craige
j821005
Level II

Re: How to assign a series of value to selected rows without using a for loop?

That's a good hint.
Thanks a lot!