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)
reading the forum I understand I need to convert DT into a matrix, but I am not able to make it work.
thanks!
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"} ) );
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:
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."));