Access count values in text facet

Hi!

How can I access the "count value" in a text facet and use it in calculations?


nr_rows = row.record.rowCount

The choices are the "value" in GREL.
What I want to do:
Calculate "count value" / value
For example: 18 / 9 = 2

In this example this would show that 2 individuals have 9 rows per record, etc.

I can easily copy and paste it into Excel, but I was curious if the "count values" are accessible in OR.

Regards,
Wolf

Hi Wolf,

I think the function you are looking for is called facetCount.

Best
Benjamin

Thank you for pointing me in the right direction, but I still don't know how to use the function.
Subject is the record key. nr_rows the number of rows per subject.

value.facetCount("value","Subject") returns 0 for records with 1 row and 1 for records with more than 1 row.

You can sorta cheat by clicking on the blue link "Facet by choice counts" and then in the next Facet Choice facet, you can click on its blue change link which will open the Expression editor and then you can modify as needed.

Facet count values will output as a Number datatype so just do your math with those Numbers such as divide / by the columns value, assuming it's also a number, and if not then use / value.toNumber() on the end, something like this:

facetCount(value, "value", "Subject") / value.toNumber()

Does that somewhat help you better for your use case?

So what you are asking in the GREL expression in your screenshot is: "How often does the value in the current row in nr_rows occur in the column Subject".

What I guess you want to ask is: "How often does a record with the same length occur?"

with(
    row.record.rowCount,
    count,
    count.facetCount("row.record.rowCount", "Subject") / count)

So what is this expression doing:

  1. We save the length of the current record in the variable count using the with control. This is just some syntactic sugar to avoid writing row.record.rowCount multiple times. And to distinguish it from the row.record.rowCount expression in facetCount.
  2. We count how often a record with the same length appears using facetCount. As the record information is the same on each column the parameter "Subject" to determine the column to evaluate the row.record.rowCount expression is arbitrary.
  3. We have to divide the result by count, because a record with a length of e.g. 3 will be counted 3 times. One time for each row it has.

So this is some generic GREL expression that works independent of the values in nr_rows.

Hope this helps.

Thank you, Thad and Benjamin, but neither of your suggestions gives me the result I want.

When I do Facet by choice counts, the second facet opens a numeric facet. I don't know why it looks different from your example.

Benjamin's function returns other multiples of the original count.

Perhaps we can leave this concrete example, which can be easily solved with other tools, and return to the more general question of whether the count is accessible as an OR object, as there is row, value, etc.?

facetCount is a Function, not an Object, in GREL. It's code lives here.
You can see what are direct objects in GREL itself.
In the expression editor, type an object name, such as row only and notice the output preview actually shows the type [object Row] and if you do that for facetCount you'll get nothing returned, and facetCount() (the function call invocation - functions end with paratheses) will return an EvalError such as "expects a choice value, an expression as a string, and a column name".
facetCount() always returns a Number, (i.e. a count of grouped choice values):

Mine looked different because my 1st version facet is a list of String values, not Number values, as is your case. When you facet on Strings or Numbers, the Facets will differ appropriately, but can always be clicked on "change" to make them display as you might want to (changing Numbers to String, vice-versa, Dates, Booleans, etc.) Or just start with our built-in Facet types that get you quickly there and modify slightly as needed.

I thought it might be the data type, but the facet by value counts still opens a numeric facet.

Now I know that the count is a function and cannot be accessed as object.
Thanks!

"Facet by choice counts" will indeed be a list of numbers (counts), which will get binned (grouped) in the display of the Numeric facet. Ignore my example that's slightly mutated, because I indeed mutated it, and you can't see that unfortunately. :slight_smile:

Hi @wolf

The fact this opens as a numeric facet is just the built in functionality when clicking the "Facet by choice counts" . If you were to click "change" on that facet, and then cut and paste the GREL there into a new "custom text facet" it would display as a text facet.

1 Like

And as an aside I find the facetCount the least intuitive and most confusing of all the GREL functions!