- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to assign a series of value to selected rows without using a for loop?
But instead of assign "a certain value", I want to assign "a series of values" to corresponding rows without a for loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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")]="";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: How to assign a series of value to selected rows without using a for loop?
Thanks a lot!