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
Mauro_Gerber
Level IV

Re: Counting values in each row

Small question, do you need it per column or over all?
"I thought about our dilemma, and I came up with a solution that I honestly think works out best for one of both of us"
- GLaDOS
itzikd
Level II

Re: Counting values in each row

for all, but it is per a row
like each row counts how many times a values appeared inside that row and then at the end of that row i write the "count" value
see my example below i added some table
txnelson
Super User

Re: Counting values in each row

Here are the formulas that are one way of calculating the counts

count = 0;
For( i = 1, i <= 6, i++,
	count = Sum( count, Contains( As Column( i ), "test" ) )
);
count;
count = 0;
For( i = 1, i <= 6, i++,
	count = Sum( count, Contains( As Column( i ), "POR" ) )
);
count;
Jim
itzikd
Level II

Re: Counting values in each row

Hey, this does not work

for example if I run it on this

PORPORPORPOR
PORPORPORPOR
testPORPORtest
testPORPORtest
PORPORPORPOR
PORPORPORPOR
testPORPORtest
testPORtesttest

 

the counts come out as 0

what I would expect when I run the "test" one is: 0,2,2,0,0,2,3 

so the output is like this:

PORPORPORPOR0
PORPORPORPOR0
testPORPORtest2
testPORPORtest2
PORPORPORPOR0
PORPORPORPOR0
testPORPORtest2
testPORtesttest3
txnelson
Super User

Re: Counting values in each row

I created in JMP the table as you provided

txnelson_0-1626373438182.png

I then created a new column and applied the following formula to that column

count = 0;
For( i = 1, i <= 4, i++,
	count = Sum( count, Contains( As Column( i ), "test" ) )
);
count;

Please note, the For() loop only goes up to 4, because there are only 4 columns in the table to process

It gives the following results

txnelson_1-1626373620048.png

I have attached the data table

I strongly suggest that you take the time to read the document, Discovering JMP, available under the Help pull down menu

 

 

 

Jim

Re: Counting values in each row

Hi,

 

Yet another way involves concatenating all the columns with a 1-click transform.

 

Since "test" is 1 character more than POR, and you've no missing data, the resultant string will increase by 1 character for every "test" contained, compared to a sting of only "POR"s.

 

To concatenate, select the columns of interest and right-click, selecting as below. Then rename the column "concat" for brevity.

 

brady_brady_0-1626392194531.png

In the example table below, there are 6 columns of interest; a string with only "POR"s would thus have 6 * 3 = 18 characters.

 

Hence, we use the formulas:

 

length(:concat) - 18 for the number of "test" == n_test.

 

and

 

6 - n_test for the number of "POR".

 

In general, for n_test we'd use (length(:concat) - 18)/k, where k is the difference in characters between the 2 phrases. Here, since k == 1, we omit it.

 

 

brady_brady_1-1626392396499.png

 

Cheers,

Brady

 

txnelson
Super User

Re: Counting values in each row

cool idea
Jim

Re: Counting values in each row

Hi,

 

Please check out JMP's "Value Labels" property, as well as the "Labels to Codes" and "Codes to Labels" columns utilities.

 

I would be remiss if I did not advocate for the use of numeric 1 for POR and 0 for test (1: test and 0: POR if you prefer). Using the Value Label column property, we can make these values APPEAR as if they are "test" and "POR", but internally, they are 0s and 1s.

 

Since the values are numeric, computations can be done using native column and row formulas. In the case of 1s and zeros, the sum gives the number of 1s, while the mean gives the proportion of ones. In this example, if we store POR as 0 and "test" as 1, we can find the number of tests in a given row simply by selecting all columns of interest, right-clicking and choosing New Formula Column > Combine > Sum from the pop-up menu.

 

Below is an image of how this looks in practice. The cells appear to contain text, as the words POR and test are left-justified, but when you click on a cell you can see that in fact JMP is storing a number internally, similar to what is done with dates and times.

 

brady_brady_1-1626442455823.png

 

By clicking on the * to the right of the column names in the Columns pane (left center of data table), we can select "Value Labels" to view the current settings of the property:

brady_brady_2-1626442486477.png

 

Cheers,

Brady

 

 

Craige_Hales
Super User

Re: Counting values in each row

Another way.

Column formula to count across rowColumn formula to count across row

This formula uses data table subscripting to get a list like {"test","POR","test","test"} for row 8.

Above I used 1::4, which turns into Index(1,4) as a shortcut. You should use {Column1, Column 2, ... Column N} instead because when I changed one of the values to "xyzzy" the formula columns' values did not update until I forced the formulas to rerun. If JMP can't tell a column references another column, it will not reevaluate.

This works better:

formula with explicit column namesformula with explicit column names

 

Craige