cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
lehoth
Level II

How to create a column that counts the total specified values form other columns?

It's easy to do this one in Excel with the "countif" function but, How can define a column that counts the total specified values from other columns?

example: I have a table of the total answers, how can I count the right answer for each question?

questionstudent 1student 2right answertotal answer
1AAA2
2ABB1
3CBA0

 

thank you very much.

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to create a column that counts the total specified values form other columns?

Let's start with this table (total_answers is there just to show correct amount):

jthi_0-1629125443471.png

1. Stack

jthi_1-1629125490967.png

2. Add formula (Column 6):

jthi_3-1629125531212.png

3. Tabulate (or summary):

jthi_4-1629125579642.png

Also take care that the column Data Types and Modeling Types are correct for the data you are modeling.

 

Example table with a bit more advanced formula which I wouldn't suggest using until you learn a bit more about JSL:

 

Names Default To Here(1);

dt = New Table("Untitled 4",
	Add Rows(5),
	Set Header Height(50),
	New Column("question", Numeric, "Ordinal", Format("Best", 12), Set Values([1, 2, 3, 4, 5])),
	New Column("student1", Character, "Nominal", Set Values({"A", "A", "A", "A", "A"})),
	New Column("student2", Character, "Nominal", Set Values({"A", "B", "B", "B", "A"})),
	New Column("student3", Character, "Nominal", Set Values({"A", "B", "C", "C", "A"})),
	New Column("student4", Character, "Nominal", Set Values({"A", "B", "C", "D", "A"})),
	New Column("student5", Character, "Nominal", Set Values({"A", "B", "C", "D", "A"})),
	New Column("right_answer", Character, "Nominal", Set Values({"A", "B", "C", "D", "E"}))
);


//Don' use if you don't have clear understanding what this does! Try first using data table method
//2::5 indicates indices of student result columns
dt << New Column("Total_answers", Numeric, Continuous, 
	<< Formula(
		N Items(Loc(Current Data Table()[Row(), 2::6], :right_answer))
	)
);

 

 

-Jarmo

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: How to create a column that counts the total specified values form other columns?

Here is one way to create the formula for this

Sum( :student 1 == :right answer, :student 2 == :right answer )
Jim
lehoth
Level II

Re: How to create a column that counts the total specified values form other columns?

what if I have many students, I tried to do a for loop based on your suggestion, but I couldn't get it.

(I also collect in other classes)

lehoth_0-1629122211708.png

 

jthi
Super User

Re: How to create a column that counts the total specified values form other columns?

Let's start with this table (total_answers is there just to show correct amount):

jthi_0-1629125443471.png

1. Stack

jthi_1-1629125490967.png

2. Add formula (Column 6):

jthi_3-1629125531212.png

3. Tabulate (or summary):

jthi_4-1629125579642.png

Also take care that the column Data Types and Modeling Types are correct for the data you are modeling.

 

Example table with a bit more advanced formula which I wouldn't suggest using until you learn a bit more about JSL:

 

Names Default To Here(1);

dt = New Table("Untitled 4",
	Add Rows(5),
	Set Header Height(50),
	New Column("question", Numeric, "Ordinal", Format("Best", 12), Set Values([1, 2, 3, 4, 5])),
	New Column("student1", Character, "Nominal", Set Values({"A", "A", "A", "A", "A"})),
	New Column("student2", Character, "Nominal", Set Values({"A", "B", "B", "B", "A"})),
	New Column("student3", Character, "Nominal", Set Values({"A", "B", "C", "C", "A"})),
	New Column("student4", Character, "Nominal", Set Values({"A", "B", "C", "D", "A"})),
	New Column("student5", Character, "Nominal", Set Values({"A", "B", "C", "D", "A"})),
	New Column("right_answer", Character, "Nominal", Set Values({"A", "B", "C", "D", "E"}))
);


//Don' use if you don't have clear understanding what this does! Try first using data table method
//2::5 indicates indices of student result columns
dt << New Column("Total_answers", Numeric, Continuous, 
	<< Formula(
		N Items(Loc(Current Data Table()[Row(), 2::6], :right_answer))
	)
);

 

 

-Jarmo
lehoth
Level II

Re: How to create a column that counts the total specified values form other columns?

thank you very much, Mr.Jthi ^^
jthi
Super User

Re: How to create a column that counts the total specified values form other columns?

Many was to do this in JMP but they do differ from excel because JMP isn't a spreadsheet.

 

Some examples with formulas:

 

N Items(Loc(Words(:"student 1"n || :"student 2"n, ""), :"right answer"n));

 

 

N Items(Loc(Current Data Table()[row(), {"student 1", "student 2"}], :"right answer"n))

 

 

Datatables: Stack table with student columns:

jthi_0-1629097997023.png

Add formula to check differences between answer and and right answer:

jthi_1-1629098080884.png

Formula for Column 5:

 

If(:right answer == :Data,
	1,
	0
)

Create tabulate/summary (you might have to change the modeling type of question to Nominal or Ordinal):

jthi_2-1629098123819.png

jthi_3-1629098144883.png

 

 

-Jarmo