# Sampling Groups and individual within the group

**URL:** <https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199>\
**Category:** New to Julia\
**Tags:** dataframes, random, splitapplycombine\
**Created:** [November 28, 2021, 2:07pm UTC](https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199 "2021-11-28T14:07:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![N\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n_a/32/31176_2.png) [@N\_A](https://discourse.julialang.org/u/N_A)\
**Post date:** [November 28, 2021, 2:07pm UTC](https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199/1 "2021-11-28T14:07:08Z")

</div>

a[:,4] represent group.

```julia
a= [1 2 3 1;5 6 7 1;1 2 3 1;1 2 4 1;1 2 3 2;1 2 3 2;1 2 4 2;1 2 4 2;1 3 4 3;1 3 4 3;1 3 4 3;1 2 3 3]
12×4 Matrix{Int64}:
 1 2 3 1
 5 6 7 1
 1 2 3 1
 1 2 4 1
 1 2 3 2
 1 2 3 2
 1 2 4 2
 1 2 4 2
 1 3 4 3
 1 3 4 3
 1 3 4 3
 1 2 3 3

```

I have grouped data so far.

I want to randomly select two individuals from each group and randomly select the two groups.Can anyone help please?

```julia
a_dat=DataFrame(a,:auto)
gb=groupby(a_dat,:4)
GroupedDataFrame with 3 groups based on key: x4
First Group (4 rows): x4 = 1
x1	x2	x3	x4
Int64	Int64	Int64	Int64
1	1	2	3	1
2	5	6	7	1
3	1	2	3	1
4	1	2	4	1
⋮
Last Group (4 rows): x4 = 3
x1	x2	x3	x4
Int64	Int64	Int64	Int64
1	1	3	4	3
2	1	3	4	3
3	1	3	4	3
4	1	2	3	3

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 28, 2021, 2:17pm UTC](https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199/2 "2021-11-28T14:17:09Z")

</div>

A simple array-based solution:

```julia
using StatsBase
using SplitApplyCombine
using DataPipes

@p begin
	splitdimsview(a, 1)
	group(_[4])
	collect()
	sample(↑, 2; replace=false) # choose two groups
	map(sample(_, 2; replace=false)) # choose two members from each
end

```

returns two groups, each with two members:

```julia
2-element Vector{...}:
[[1, 2, 3, 1], [5, 6, 7, 1]]
[[1, 2, 3, 2], [1, 2, 3, 2]]

```

---

<div class="post-metadata">

**Author:** ![N\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n_a/32/31176_2.png) [@N\_A](https://discourse.julialang.org/u/N_A)\
**Post date:** [November 28, 2021, 2:31pm UTC](https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199/3 "2021-11-28T14:31:12Z")

</div>

Hi, Out of three groups; 1, 2, 3 ; I want two randomly selected groups. And within these two groups I want two randomly selected elements from three elements. The final output must look similar to below

```julia
1 3 1
5 7 1
1 3 2
1 2 2

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 28, 2021, 2:45pm UTC](https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199/4 "2021-11-28T14:45:16Z")

</div>

One way that outputs the result as a dataframe, inspired by [this post](https://discourse.julialang.org/t/how-to-sample-rows-from-a-dataframe/19782/2).

_(continuation of your code:)_

```julia
using StatsBase

ix2 = sample(1:length(gb), 2, replace=false)
db = vcat([gbi[sample(axes(gbi, 1), 2; replace=false), 1:3] for gbi in gb[ix2]]...)

 Row │ x1 x2 x3    
     │ Int64 Int64 Int64 
─────┼─────────────────────
   1 │ 1 2 4
   2 │ 5 6 7
   3 │ 1 3 4
   4 │ 1 2 3

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [November 28, 2021, 2:54pm UTC](https://discourse.julialang.org/t/sampling-groups-and-individual-within-the-group/72199/5 "2021-11-28T14:54:36Z")

</div>

All resulting members in a single matrix:

```julia
@p begin
	splitdims(a, 1)
	group(_[4])
	collect()
	sample(↑, 2; replace=false)
	map(sample(_, 2; replace=false))
	mapmany(_, __[1:3])
	combinedims()
	permutedims()
end

```

However, note that it’s often easier to work with data when each “individual” (e.g., group member in your case) is separate. Eg, vector-of-vectors vs matrix.
