cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Have your say in shaping JMP's future by participating in the new JMP Wish List Prioritization Survey
Choose Language Hide Translation Bar
nthai
Level III

How to loop through rows from groups created by 2 columns

Hi all,

 

i have a pretty simple question but unable to find the exact answer from the forum. Here's my sample data table:

nthai_1-1675063870291.png

 

Basically it has 4 columns, and i need to loop through each of combination items generating from "group 1" and "group 2", but if and only when "item" # is different then open the all  file path in "path" column. For example on first 2 rows, we have the combination is "aA", and because items also different so the script need to open both file path 1 and 2.  

14 REPLIES 14
nthai
Level III

Re: How to loop through rows from groups created by 2 columns

I manage to find one by create another column, "Unique":

 

new_col = dt << New Column("Unique", Numeric, Continuous, << Set Each Value(
	Col Sum(1, :g1, :g2); 
));



rows_to_open = dt << Get Rows Where(:ValidFile == 1 & :Unique != 1);

nthai_0-1675150798052.png

 

jthi
Super User

Re: How to loop through rows from groups created by 2 columns

You will have to modify the ValidFile calculations and possibly the row selection depending on how you build the ValidFile check.

new_col = dt << New Column("ValidFile", Numeric, Continuous, << Set Each Value(
	If(Col Sum(1, :g1, :g2, :i) == 1 & Col Number(:i, :g1, :g2) > 1,
		1
	,
		0
	);
));
-Jarmo
Ressel
Level VI

Re: How to loop through rows from groups created by 2 columns

@jthi, I just want to ask a few questions with regards to the last part of the code, i.e., I don't understand it.

 

file_list = {};
For Each({file_path}, filepaths_to_open,
	Insert Into(file_list, Open(file_path));
);

 

 

 

file_list = {};

 

This is an empty list. The loop will fill this up via the Insert Into(file_list, Open(file_path)). Why can't one just use the list already contained in filepaths_to_open? Also, I don't understand how the Open(file_path) will insert something into the list. Isn't this just a function to open a file? How can at it all contribute to a list?

 

{file_path}

This, I also don't understand. Is this just a placeholder for the actual filepaths?

 

Maybe you have a reference explaining these things? Thank you very much already in advance.

jthi
Super User

Re: How to loop through rows from groups created by 2 columns

I think those are easiest to understand by writing a demo script ( I modified For Each a bit to add idx, it might make it a bit easier to understand how it works)

 

Names Default To Here(1);

filepaths_to_open = {"$SAMPLE_DATA/Big Class.jmp", "$SAMPLE_DATA/Probe.jmp", "$SAMPLE_DATA/Iris.jmp"};
show(filepaths_to_open);
file_list = {};
show(file_list);
For Each({file_path, idx}, filepaths_to_open,
	show(idx, file_path);
	Insert Into(file_list, Open(file_path));
	show(file_list);
);

 

  • I use file_list to collect the references to the data tables which are opened (I don't need the filepaths as I already have them in the filepaths_to_open, but I don't have references)
  • Open(file_path) will return a reference to the opened file and Insert Into will then add that reference to the file_list.
  • The {filepath} comes from how For Each function should be used. Basically it will loop over filepaths_to_open and will return each element (one by one) in file_path variable.

 

filepaths_to_open = {"$SAMPLE_DATA/Big Class.jmp", "$SAMPLE_DATA/Probe.jmp", "$SAMPLE_DATA/Iris.jmp"};
file_list = {};
idx = 1;
file_path = "$SAMPLE_DATA/Big Class.jmp";
file_list = {DataTable("Big Class")};
idx = 2;
file_path = "$SAMPLE_DATA/Probe.jmp";
file_list = {DataTable("Big Class"), DataTable("Probe")};
idx = 3;
file_path = "$SAMPLE_DATA/Iris.jmp";
file_list = {DataTable("Big Class"), DataTable("Probe"), DataTable("Iris")};

Hopefully answer some of the questions.

 

 

 

-Jarmo
Ressel
Level VI

Re: How to loop through rows from groups created by 2 columns

Yes, that was actually very useful. The file_list variable was confusing, since it is not strictly needed to open to the files (but; I take it; that it is useful later on when closing all files again (?), for example, or some other manipulation). I have to admit that it also took me some time to wrap my head around the "file_path" variable, which is assigned to a different file path at every iteration, and that it doesn't matter what it is called. (In my head, I just interpret it now as a vehicle that picks one file path after the other from the filepaths_to_open list and lets the user do "something" with it.)

 

Thank you! Every week a tiny growth of knowledge is better that none at all.

 

P.s.: The below I was only able to write based on my understanding of your explanations. Again, very useful.

 

Names Default To Here( 1 );

filepaths_to_open = {"$SAMPLE_DATA/Big Class.jmp", "$SAMPLE_DATA/Probe.jmp", "$SAMPLE_DATA/Iris.jmp"};

For Each( {file_path}, filepaths_to_open, Open( file_path ) );