Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于R中的平均值对数组进行随机分组_R_Random_Grouping - Fatal编程技术网

基于R中的平均值对数组进行随机分组

基于R中的平均值对数组进行随机分组,r,random,grouping,R,Random,Grouping,我在下面给出了一个数据框。第一列是第二列中给出的测量值列表的ID。我想将ID(每组8个)随机分为3组多次,直到组值平均值之间的差异最小,即对最终组平均值进行方差分析,得出接近1.0的p值 ID Value 01F 128.1 02F 196.5 03F 118.3 04F 165.5 05F 105.0 06F 187.5 07F 105.5 08F 148.6 09F 147.2 10F 110.9 11F 109.9 12F 136.5 13F 105.4 14F 196.3 15F 199

我在下面给出了一个数据框。第一列是第二列中给出的测量值列表的ID。我想将ID(每组8个)随机分为3组多次,直到组值平均值之间的差异最小,即对最终组平均值进行方差分析,得出接近1.0的p值

ID Value
01F 128.1
02F 196.5
03F 118.3
04F 165.5
05F 105.0
06F 187.5
07F 105.5
08F 148.6
09F 147.2
10F 110.9
11F 109.9
12F 136.5
13F 105.4
14F 196.3
15F 199.2
18F 174.8
19F 138.9
21F 128.1
22F 196.5
26F 187.5
27F 105.5
28F 148.6
29F 147.2
30F 110.9

谢谢。

请告诉我这样做是否有效

#Initiate data
output = df
output$group = rep(c("grp1", "grp2", "grp3"), each = 8)
p = summary(aov(Value~group, output))[[1]][["Pr(>F)"]][1]

set.seed(42)
#Iterate until a suitable output is found
while (p < 0.95){ #Choosing higher value could take more time
    output$group = sample(output$group)
    p = summary(aov(Value~group, output))[[1]][["Pr(>F)"]][1]
}

#At this point, 'output' is the answer you want. You can split it by group
split(output, output$group)

#Check p-value
summary(aov(Value~group, output))
#            Df Sum Sq Mean Sq F value Pr(>F)
#group        2     41    20.7   0.016  0.984
#Residuals   21  27015  1286.4 

你对群体差异的准确测量是什么意思。第1组和第2组,第1组和第3组,第2组和第3组之间的绝对值之差之和?非常感谢d.b.我认为这是可行的。您能列出具有ID和最佳p值的组吗?另外,不要寻找随机分组的p值,比如通过将
output$group=sample(rep(c(“grp1”、“grp2”、“grp3”),每个=8))
更改为
output$group=sample(output$group)
再次感谢d.b。感谢您的Snoom的效率变化。
df = structure(list(ID = c("01F", "02F", "03F", "04F", "05F", "06F", 
"07F", "08F", "09F", "10F", "11F", "12F", "13F", "14F", "15F", 
"18F", "19F", "21F", "22F", "26F", "27F", "28F", "29F", "30F"
), Value = c(128.1, 196.5, 118.3, 165.5, 105, 187.5, 105.5, 148.6, 
147.2, 110.9, 109.9, 136.5, 105.4, 196.3, 199.2, 174.8, 138.9, 
128.1, 196.5, 187.5, 105.5, 148.6, 147.2, 110.9)), .Names = c("ID", 
"Value"), class = "data.frame", row.names = c(NA, -24L))