Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Count array

  • sjsls2016
  • sjsls2016's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #143993 by sjsls2016
Count array was created by sjsls2016
Hi there. I would like to ask how is that possible to count the answers chosen on an array question.
supposing I have q1 as question code then subquestion code is sq001 then the answer codes are 1,2,3,4,5
I wanted to know how many(count) answers he/she gave on a specific subquestion and answer.

I have done like this one
Low = {sum(if(q1_sq001 == 1,1,0), if(q1_sq002 == 1,1,0), if(q1_sq003 == 2,1,0))}
High = {sum)if(q1_sq001 == 2,1,0), if(q1_sq002 == 2,1,0)))}
Medium = {......}

The result would be
Low = 5
High = 10
Medium = 4

This formula works on radio list, but now it's array type and I don't know what to change in the formula.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Online
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #143996 by Joffm
Replied by Joffm on topic Count array
Hi, sjsls2016,
first a few hints about different ways to count.
I wrote this a few days ago in another thread.

Four ways to count:
A1Q1 is the question code. And example with only 5 subquestions.

1. The longest, but easiest to understand:
{sum(if(A1Q1_SQ001==1,1,0),if(A1Q1_SQ002==1,1,0),if(A1Q1_SQ003==1,1,0),if(A1Q1_SQ004==1,1,0),if(A1Q1_SQ005==1,1,0))}
You sum up:
If the 1st subquestion is equal 1, you sum 1, else 0
If the 2nd subquestion is equal 1, you sum 1, else 0
...

2. The use of boolean expression
{sum(A1Q1_SQ001==1,A1Q1_SQ002==1,A1Q1_SQ003==1,A1Q1_SQ004==1,A1Q1_SQ005==1)}
Again you sum up.
And you know that a boolean comparison is equal 1, if true, 0, if false.
So again you sum up the five subquestion with each comparison has a value of 1 or 0.

3. Use of the function countifop (Count the number of answered questions in the list which pass the criteria (arg op value))
{countifop('==',1,A1Q1_SQ001,A1Q1_SQ002,A1Q1_SQ003,A1Q1_SQ004,A1Q1_SQ005)}
You see the first two parameters, which say "equal" and "1".
Then the five subquestions.

4. Use of countifop and "that"
{countifop('==',1,that.A1Q1.NAOK)}
You count the appearance of "=1" in the whole question A1Q1.


That so far.
What you show us is the calculation of the values "1" resp. "2" in an array. (Question q1 with three subquestions).

This seems to work in a simple array, but not in a list (radio) - there are no subquestions.

Maybe you want to expnd your formula from array to array (numbers).
Then you have to consider that you have to sum rows and columns.
So it can be :
low={sum(if(q1_SQ001_SQ001==1,1,0),if(q1_SQ001_SQ002==1,1,0),if(q1_SQ001_SQ003==1,1,0),if(q1_SQ002_SQ001==1,1,0),...
You have to sum all value in x-scale and y-scale.
In this case I'd recommend the "countifop"-function.

Best way to clarify your needs:
Provide a small sample survey.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: sjsls2016
The topic has been locked.
  • sjsls2016
  • sjsls2016's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144017 by sjsls2016
Replied by sjsls2016 on topic Count array
Thank you very much for your interest in helping me. The formula showed an error, undefined variable.
But anyway I added a lss file, hope you will help me. Thank you.

File Attachment:

File Name: limesurvey...5936.lss
File Size:16 KB
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Online
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #144027 by Joffm
Replied by Joffm on topic Count array
Hi,
do you want to know the sum of these special answers in the subquestions.

You did not assign numerical answer codes. So the formula will fail.
So it has to be like
Low = {sum(if(q1_sq001 == "A1",1,0), if(q1_sq002 == "A1",1,0), if(q1_sq003 == "A2",1,0))}
You compare with the code.

I will send the formula after I had a coffee. assuming
Answer 1 with code A1 = 1
Answer 2 with code A2 = 2
...

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Online
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #144030 by Joffm
Replied by Joffm on topic Count array
Hi, sjsls2016,
as I told you, you shouldn't have the answer codes in an array like "A1", "A2", ... though it is the default.
Okay, in this sample I assume the answer options are nominal.
But often the answer options are ordinal or interval, to be able to calculate a mean or so. In this case it ist better to have numbers.

And attached the survey with the formulas.
I still don't know what you want to count or sum?

Just the amount of "high" vs. "low" answers?
Or a value of "high" and "low" answers.

Nevertheless I included both.
Now you can adapt it to your needs.

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose