a[:,4] 代表組。
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
到目前為止,我已經對資料進行了分組。
我想從每組中隨機選擇兩個人并隨機選擇兩個組。有人可以幫忙嗎?
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
uj5u.com熱心網友回復:
例如,您可以這樣做:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/369020.html
上一篇:用行中的閾值過濾掉值并獲得平均值
下一篇:洗掉資料框中的特定行
