# Randomly selecting a member from a repeat group (with side conditions)

Hi all,

I am implementing a household survey using ODK/SurveyCTO. I have a repeat
group which collects names and sexes of household members and would like to
randomly select the name of one boy from this repeat group. I have troubles
figuring out how to do it.

As explained here
without the condition that this person must be a boy this would be easy:
Just generate a random position of the repeat group and take the household
member at that position. But in my case, this person might be a girl and
repeating the procedure doesn't guarantee that the next time it will be a
boy.

Is there any way to do this? I have been trying to generate a new repeat
group with only the boys, but I haven't succeeded. The main problem for
this seems to be that I can not pass on information from one instance to
the next in a repeat group (from position(..) - 1 to position(..)). With
this, you could implement any "loop" to solve these kind of problems.

Thanks!
Wouter

Hi Wouter,

Here's a method that might work, though I'm not positive...

In your repeat, add an "isboy" field with a calculation like (modified to
test gender as appropriate to your gender field):

if(\${gender}=1, 1, 0)

Also add a "boyname" field with a calculation like (modified again in the
same way):

if(\${gender}=1, \${name}, '')

(Note that the second parameter to the if function is a pair of single
quotation marks.)

Now, outside the repeat you can have a "numboys" field with a calculation
like:

sum(\${isboy})

And a "boynames" field with a calculation like:

join(' ', \${boyname})

(Note that the first parameter to the join function is a single space
inside single quotation marks. If you need to pull the boy's name from a
choice label, you'd call the jr:choice-name function in the above.)

So here you should have the number of boys and a list of their names. The
question is whether count-selected(\${boynames}) returns the number of boys
or the total number of repeats. If the former, then you can use
random-once() and selected-at() to draw one of the boys; if the latter,
then this method won't work because the "boysnames" list still includes
entries (though blank) for the girls. If that's the case, then what we
really need is a way to collapse multiple spaces down to one (since, in the
space-separated list of names, there are multiple spaces for girls); post
back and we'll see if somebody else has an idea for how to do that. (We
might need to add a replace function of some sort... Such a need has come
up several times before.)

Best,

Chris

··· On Thu, Jan 2, 2014 at 3:21 AM, Wouter Gelade wrote:

Hi all,

I am implementing a household survey using ODK/SurveyCTO. I have a repeat
group which collects names and sexes of household members and would like to
randomly select the name of one boy from this repeat group. I have troubles
figuring out how to do it.

As explained here
without the condition that this person must be a boy this would be easy:
Just generate a random position of the repeat group and take the household
member at that position. But in my case, this person might be a girl and
repeating the procedure doesn't guarantee that the next time it will be a
boy.

Is there any way to do this? I have been trying to generate a new repeat
group with only the boys, but I haven't succeeded. The main problem for
this seems to be that I can not pass on information from one instance to
the next in a repeat group (from position(..) - 1 to position(..)). With
this, you could implement any "loop" to solve these kind of problems.

Thanks!
Wouter

## --

You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an

Hello Chris,

Has anyone been able to come up with a solution to this?

currently in a form am designing, am able to pull out the choice list for
men who are at a certain age to respond to a certain question but this
comes with blank spaces for the women who have also taken up instance
spaces in the repeat group count.

Desperately need a solution, to be able to concatenate for men only without
the blank spaces.

Thanks!!!