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

Counting values in each row

I have a script with col 1-6 in a list

I am trying to write a JSL script that will count how many times the word POR appear, and how many times the word test appears

example below (did it manually the last 2 columns)

itzikd_0-1626357715790.png

reading the forum I understand I need to convert DT into a matrix, but I am not able to make it work.

 

thanks!

12 REPLIES 12

Re: Counting values in each row

I like this, Craige.

 

It looks like the design matrix feature can handle this idea, too-try this formula to count the number of "a"s.

 

dt = :Col1 << get data table;
Sum( Design( dt[Row(), {:Col1, :Col2, :Col3, :Col4, :Col5}], {"a"} ) );

 

txnelson
Super User

Re: Counting values in each row

Very cool........but I think ya stepped a bit into the great abyss
Jim

Re: Counting values in each row

I know... but it is a surprisingly good method from a time standpoint. Interestingly the concat method, as odd as it is, is really fast as well.

 

For example, for a 200,000 row x 10 column table (compressed file attached) of these values, here are the runtimes:

brady_brady_0-1626463751349.png

 

Names Default To Here( 1 );

dt = Current Data Table();

t1 = HP Time();

dt << new column("xx",
	formula(sum( design (dt[row(), {:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8, :col9, :col10}], {"test"})))
);

t2 = hptime();

dt << New Column( "xx",
	formula(
		count = 0;
		dt = :Col1 << get data table;
		For( i = 1, i <= 10, i++,
			If( Column( dt, i )[Row()] == "test", count	++)
		);
		count;
	)
);

t3 = hptime();

dt << new column("xx", formula(nrow(loc(dt[row(), {:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8, :col9, :col10}], "test"))));

t4 = hptime();

dt << new column("concat", character, formula(Col1 || :Col2 || :Col3 || :Col4 || :Col5 || :Col6 || :Col7 || :Col8 || :Col9 ||:Col10));
dt << new column("xx", formula(length(:concat)-30));

t5 = hptime();

print(evalinsert("Design matrix method:	^(t2-t1)/1e6^ seconds."));
print(evalinsert("Looping method: 			^(t3-t2)/1e6^ seconds."));
print(evalinsert("Nrow(Loc()) method:		^(t4-t3)/1e6^ seconds."));
print(evalinsert("Concat method: 				^(t5-t4)/1e6^ seconds."));