Subscribe Bookmark RSS Feed

Difficulty over looping over two columns to create third column

IrisK

Occasional Contributor

Joined:

Aug 10, 2017

 

dtt=Data table("inputdata");
//option 1, not working as desired
colc=dtt<<NewColumn("codefin",Formula(Char(:dates)||"_"||Char(:codes)));
IDs=dtt<<New Column( "ID",
		Numeric,
		Continuous,
		Format( "Best", 8 ),
		Formula(
			If( Row() == 1,
				1,
				If( :codefin== Lag( :ID, 1 ),
					Empty(),
					Lag( :ID, 1 ) + 1
				)
			)
		)
	);
	
//option 2, not working
/*
//I started the following for loop, but I'm not sure how to complete it.
num= List(1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28); only for 7 buildings and 4 days, for now.
For( n = 1, n <=7, n++,i=1, i<=4, i++, k=1, k<=28, k++
	If((:codes(n)) & (:dates(i))),print(num(k)),28);
	
	,n(i);
	colname = Column( n ) << 

*/

Hello,

 

I'm fairly new to JSL scripting, and I'm seeking help is writing a somewhat challenging if and/or for loop.

So I want to loop over two columns called “codes” and “dates” of different values to generate a specific number between 1-28 in a new column called “ID”.

The first columns has a repetition of numeric values ranging from 1-7. The second column has repetitive dates in this format, dd/mm/yyyy "dates". The third column is called “TestID” for the outputs.

 

My goal is to create a new column ”ID” that generates values 1-28 base on the following match.

that is when/if codes=1 & dates=1st date , the value in the new column is 1

then, when/if codes=1 & dates= 2nd date, the value in the new column is 2

then, when/if codes=1 & dates=3rd date ...newcolumn=3

then, when/if codes=1 & dates=4th date ...newcolumn=4

all over again...

then, when/if codes=2 & dates=3rd date ...newcolumn=5

then, when/if codes=2 & dates=4th date ...newcolumn=6

.

.

.

until,

then, when/if codes=7 & dates=4th date ...column=28

 

So far I have only been able to successfully write the attached above script , but it's not working as I desired.

 

This may be a bit challenging, but I'll apreciate any help.

 

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

I am not sure if what I interpreted from your communication is correct, but if it is, here is a formula that will generate your 1-28 values, assuming there are no more than 4 valid values for "Dates" and "Codes" only have 1-7 values.

 

 

If( Row() == 1,
	Summarize( bygroup = by( :dates ) );
	If( bygroup[1] == "",
		newbygroup = Remove( bygroup, 1 ),
		newbygroup = bygroup
	);
);
(Loc( newbygroup, :dates ) - 1) * 7 + :codes;
Jim
7 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

Solution

I am not sure if what I interpreted from your communication is correct, but if it is, here is a formula that will generate your 1-28 values, assuming there are no more than 4 valid values for "Dates" and "Codes" only have 1-7 values.

 

 

If( Row() == 1,
	Summarize( bygroup = by( :dates ) );
	If( bygroup[1] == "",
		newbygroup = Remove( bygroup, 1 ),
		newbygroup = bygroup
	);
);
(Loc( newbygroup, :dates ) - 1) * 7 + :codes;
Jim
IrisK

Occasional Contributor

Joined:

Aug 10, 2017

Thank you Jim,

this did the job perfectly!

 

Best,

Iris

IrisK

Occasional Contributor

Joined:

Aug 10, 2017

Hello Jim,

 

I'm sorry for coming back to this. I know I accepted this answer more than a week agao, but I just realised that the script doesn't work completely right.

 

Let me explain:

 

So you complete understood what I want to do. I need to basically create a set of IDs that are base in the changing values of the codes column and the dates column.

 

The goal is exactly this: if the are 4 dates are repeadetly in the "dates" column, and there are 1-7 numbers repeated in the "codes" column. I want the "ID" column to come out like this:

Date1 + code=1 > ID= 1

date2 + code=1 > ID= 2

date3 + code=1 > ID=3

date4 + code=1> ID=4

 

date1 + code=2 > ID=5

date2+ code=2> ID=6

date3 + code=2 >ID=7

.

.

.

 

Right now the script you help me only allow for the IDs  to be correct only for date1 of all the columns because the script is dependind on the number in the code column. so it's doing this:

date1 + codes=1 > ID=1  GOOD

date2 + codes=1 > ID= 8  BAD

date3 + codes=1 > ID=15   BAD

and so on...

 

I really hope this make sense. The script is on the right path! However, I realy don't  know how to make it correct by depending on both the change of dates and codes. Do you have an idea for can be done?  

My team and I, unfortunately, really have to get the data in ths format.

 

 

txnelson

Super User

Joined:

Jun 22, 2012

@IrisK

I am not able to understand what you need.  Could you provide a table that shows the values of Dates and Codes, and what the resulting value of ID would be?

Jim
IrisK

Occasional Contributor

Joined:

Aug 10, 2017

New Table( "inputdata",
	Add Rows( 308 ),
	New Column( "dates",
		Character,
		Nominal,
		Set Values(
			{"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "07/10/2017", "07/17/2017", "07/17/2017", "07/17/2017",
			"07/17/2017", "07/17/2017", "07/17/2017", "07/24/2017", "07/24/2017",
			"07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017", "07/24/2017",
			"07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017", "07/10/2017",
			"07/10/2017", "", ""}
		)
	),
	New Column( "codes",
		Numeric,
		Continuous,
		Format( "Best", 8 ),
		Set Values(
			[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
			3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
			4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
			5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 2, 2,
			2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
			2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
			1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
			4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 2,
			2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
			1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4] 
		)
	));

Yes. I'll paste the short script  with the if loop below. and the data set is attached above.

 

Thank you for looking at this already!

 

Script:

dtt=Data table("inputdata");

//option 1, not working as desired

colc=dtt<<NewColumn("codefin",Formula(Char(:dates)||"_"||Char(:codes)));

IDs=dtt<<New Column( "ID",

Numeric,

Continuous,

Format("Best", 8 ),

Formula(If( Row() == 1,

Summarize( bygroup = by( :dates) );

If( bygroup[1] == "",

newbygroup = Remove( bygroup, 1 ),

newbygroup = bygroup

);

);

(Loc( newbygroup, :dates ) - 1) * 7 + :codes;),

get selected

);

txnelson

Super User

Joined:

Jun 22, 2012

Try this formula:

If( Row() == 1,
	Summarize( bygroup = by( :dates ) );
	If( bygroup[1] == "",
		newbygroup = Remove( bygroup, 1 ),
		newbygroup = bygroup
	);
);
(:codes - 1) * 4 + Loc( newbygroup, :dates );

My previous version was Dates based, but it should have be Codes based.

Jim
IrisK

Occasional Contributor

Joined:

Aug 10, 2017

Thank you very much Jim.

This worked perfectly!

 

-IrisK