用于多个子阵列的循环迭代

时间:2017-08-07 19:30:29

标签: r loops

我有变量赋值

N <- 10

H <- 10

K <- 2 # number of subarrays 

perms <- 10

我按如下方式设置了一个空数组:

pop <- array(dim = c(c(perms, N), K))

然后我分配字符标签:

haps <- as.character(1:H)

现在,我指定了概率:

probs <- rep(1/H, H) 

然后我创建了一个&#39; for&#39;循环:

for(j in 1:perms){

    for(i in 1:K){
    pop[i,j] <- sample(haps, size = N, replace = TRUE, prob = probs)
    }
}

&#39;弹出&#39;现在应该在两个子数组中包含来自1:H的字符标签。相反,我最终得到一个错误&#34;矩阵上的下标数量不正确。&#34;

我不确定为什么R会产生错误。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您为3维向量series1.Points.DataBindXY() 提供了2个下标。此外,即使您添加了第3个下标,因为订单错误,样本的尺寸也不会与pop切片的尺寸相匹配。

pop
  

[1] 10 10 2

你可能想要这个:

dim(pop)
for(j in 1:perms){

  for(i in 1:K){
    pop[1:10,j,i] <- sample(haps, size = N, replace = TRUE, prob = probs)
  }

}

pop

虽然我觉得您希望, , 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] "6" "8" "8" "6" "3" "3" "1" "9" "7" "8" [2,] "7" "10" "5" "3" "7" "10" "7" "1" "5" "8" [3,] "9" "7" "1" "4" "1" "3" "2" "7" "6" "5" [4,] "6" "8" "9" "4" "9" "7" "10" "9" "7" "2" [5,] "1" "3" "2" "6" "10" "3" "3" "9" "10" "6" [6,] "9" "3" "8" "1" "6" "6" "4" "8" "8" "9" [7,] "9" "2" "2" "2" "3" "9" "8" "6" "10" "10" [8,] "9" "5" "3" "8" "3" "4" "1" "6" "8" "4" [9,] "10" "8" "1" "3" "10" "2" "5" "10" "6" "4" [10,] "2" "1" "8" "10" "5" "5" "7" "8" "7" "6" , , 2 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] "1" "10" "10" "7" "3" "6" "3" "2" "4" "1" [2,] "8" "5" "10" "8" "3" "6" "6" "6" "8" "2" [3,] "1" "6" "1" "5" "6" "3" "6" "1" "7" "9" [4,] "7" "10" "5" "5" "7" "5" "3" "3" "10" "1" [5,] "7" "3" "6" "8" "3" "9" "6" "2" "7" "3" [6,] "1" "9" "4" "9" "1" "1" "10" "4" "3" "9" [7,] "9" "3" "10" "1" "2" "2" "2" "3" "5" "4" [8,] "8" "10" "8" "6" "9" "6" "9" "9" "2" "4" [9,] "4" "6" "1" "1" "6" "5" "6" "6" "10" "3" [10,] "5" "9" "9" "7" "9" "6" "4" "2" "10" "9" 中的数字和haps中的数字成为字符,但我认为您有理由。

这是样本的逐列分配。我不确定它是否重要,但如果你更喜欢行,那么你可以像这样做索引:

pop