# Aggregating from a Multiple Choice list to create a histogram

Trying to have a list with options from 0, 1, 2, … 12 and a second one of R, 0, 1 , 2, 3, 4.

The aggregate code is:
MC2=input2

numberList(“M_{c0}”): aggregate(when MC2.isSelected(R) R otherwise 1000)
numberList(“M_{c1}”): aggregate(when MC2.isSelected(1) 1 otherwise 1000)
numberList(“M_{c2}”): aggregate(when MC2.isSelected(2) 2 otherwise 1000)
numberList(“M_{c3}”): aggregate(when MC2.isSelected(3) 3 otherwise 1000)
numberList(“M_{c4}”): aggregate(when MC2.isSelected(4) 4 otherwise 1000)

(1) How do I handle the letter “R” without being flagged as a type mismatch? (it does work when I test it… but just wondering)
(2) The last entry of the list does not generate a value, and hence no bar in the histogram. All but M_c4 in the one list or M_c12 in the other list return a value and can create a histogram. The commands were copy and pasted with the index updated after. Any ideas why the last entry does not return a value?

I did the histogram code it two ways making the individual bars that then are graphed side by side:
\operatorname{polygon}\left(\left[12.5,12.5,13.5,13.5\right],\ M_{c12}\cdot\left[0,1,1,0\right]\right)

\operatorname{histogram}\left(M_{c12},\ 1\right)

Much appreciated

isSelected takes an index value — isSelected(4) means the fourth multiple-choice option is selected, not the choice with the value 4. So, you’re actually going to want something like:

# Aggregate choice values:
numberList(M_{c0}): aggregate(when MC2.isSelected(1) R otherwise 1000)
numberList(M_{c1}): aggregate(when MC2.isSelected(2) 1 otherwise 1000)
numberList(M_{c2}): aggregate(when MC2.isSelected(3) 2 otherwise 1000)
...


That’s half of how you avoid errors from using “R”… The other half will be to assign a special value to R, say R=-1. Then, that first aggregate call will get you a list of -1's (when choice 1 is selected, otherwise 1000's)

Strange that the last entry isn’t generating a value… It might be helpful to see your specific graph / activity, maybe there’s something lurking like a duplicate variable name.

Your first histogram code isn’t going to get you what you’re expecting. Since M_{c12} is a list of values, it’s going to be matched up the other lists in the polygon, and multiply element-wise against your y-coordinate list to make it [0, M_{c12}[2], M_{c12}[3], 0]. You can turn that into a list of rectangles where the height of each is given by the elements in M_{c12} with list comprehension, but I don’t think that’s what you want, either! Demo: Graphing Calculator

I’m actually not 100% sure I know what you’re hoping for from this screen. Your second option for histogram code will spit out a rectangle at 12 and a rectangle at 1000, with heights matching the count of each of those values within the list — Oh! Are you planning 12 such histogram(M_{##}) expressions, to make a barchart-like visual? That would work.
You could also reduce your aggregation into a single list in that case:

# In CL:
R = -1
numberList(L): aggregate( when MC2.isSelected(1) R when MC2.isSelected(2) 1 when MC2.isSelected(3) 2 otherwise 100
# In the graph: histogram(L)


Thankyou! I was not sure how the isSelected command actually worked . This is helpful!

Many thanks for your thoughts! Yes - I was planning to make 12 bars adjacent to each other…I had found an example shared in this forum so the copy and paste feature made it relatively simple albeit I am still working to learn CL. I will try your suggestion of a single list aggregation. To see the code - the activity is at J4JSJ5.