cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
PowerOx327
Level I

How do I condition a for loop to work only for a particular set of rows

Hi,

I'm trying to manipulate a data table with a formula, but I only want it to read for a few rows.

 

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Cars.jmp" );

r = Current Data Table() << Get rows where( :Make == "Mazda");

For( i = r[1], i <= r, i++,
:HeadIC[i] = :HeadIC[i]/2

);

Show(r);

How do I make it so that it only iterates for those selected rows?

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
SamGardner
Level VII

Re: How do I condition a for loop to work only for a particular set of rows

In your example, since you have made the vector with the row numbers, you can also use "For Each()".  This would be faster, especially for a larger data table (with may rows).

 

 

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Cars.jmp" );

r = Current Data Table() << Get rows where( :Make == "Mazda");

for each({cr,ii}, r,
	:headic[cr] = :headic[cr]/2
);

 

 

 

View solution in original post

jthi
Super User

Re: How do I condition a for loop to work only for a particular set of rows

For Each() is new function introduced in JMP16, so if you are using older version of JMP you won't find it in scripting index (and it will give an error). You could convert it to for-loop if you wanted to:

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Cars.jmp" );

r = Current Data Table() << Get rows where( :Make == "Mazda");

/*For Each({cr,ii}, r,
	:headic[cr] = :headic[cr]/2
);*/
For(ii = 1, ii <= N Items(r), ii++,
	cr = r[ii];
	:headic[cr] = :headic[cr]/2;
);

 

For Each(names, container, locals, body) (jmp16 help) 

-Jarmo

View solution in original post

6 REPLIES 6
Ressel
Level VI

Re: How do I condition a for loop to work only for a particular set of rows

Like this?

 

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Cars.jmp" );

For Each Row( If ( :Make == "Mazda", :HeadIC = :HeadIC/2 ));

 

SamGardner
Level VII

Re: How do I condition a for loop to work only for a particular set of rows

In your example, since you have made the vector with the row numbers, you can also use "For Each()".  This would be faster, especially for a larger data table (with may rows).

 

 

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Cars.jmp" );

r = Current Data Table() << Get rows where( :Make == "Mazda");

for each({cr,ii}, r,
	:headic[cr] = :headic[cr]/2
);

 

 

 

Ressel
Level VI

Re: How do I condition a for loop to work only for a particular set of rows

Please demystify this for me:

 

for each({cr,ii}, r,
	:headic[cr] = :headic[cr]/2
);

// specifically, where do {cr,ii} come from and what do they do?

 

Not a coder. Couldn't find this in the Scripting Index. Haven't checked the Scripting Guide yet.

PowerOx327
Level I

Re: How do I condition a for loop to work only for a particular set of rows

Hi, I'm unsure how to use that function, I cant find it in the scripting index, can you show an example? When I try to run the code that you gave, errors pop up.

jthi
Super User

Re: How do I condition a for loop to work only for a particular set of rows

For Each() is new function introduced in JMP16, so if you are using older version of JMP you won't find it in scripting index (and it will give an error). You could convert it to for-loop if you wanted to:

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Cars.jmp" );

r = Current Data Table() << Get rows where( :Make == "Mazda");

/*For Each({cr,ii}, r,
	:headic[cr] = :headic[cr]/2
);*/
For(ii = 1, ii <= N Items(r), ii++,
	cr = r[ii];
	:headic[cr] = :headic[cr]/2;
);

 

For Each(names, container, locals, body) (jmp16 help) 

-Jarmo
Ressel
Level VI

Re: How do I condition a for loop to work only for a particular set of rows

That's strange. Both work just fine for me. Try again posting the full code of each solution into a separate script window. Close everything else. Run first the code from one script window, confirming that the code does what it is supposed to do. Then, close everything again apart from the other script window containing the code you still need to test.