Home > Enterprise >  How can I sample removing some groups randomly and some individuals within group randomly?
How can I sample removing some groups randomly and some individuals within group randomly?

Time:11-29

a[:,4] represent group.

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?

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   1Last 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

CodePudding user response:

You can e.g. do:

gb2 = gb[sample(1:length(gb), 2, replace=false)] # sample 2 groups
combine(gb2, sdf -> sdf[sample(1:nrow(sdf), 2, replace=false), :]) # sample 2 observations per group
  • Related