Arrays 在多个子阵列之间移动行

Arrays 在多个子阵列之间移动行,arrays,r,Arrays,R,我的问题来源于我之前提出的一个问题,但没有包括我自己的代码(我最初应该这样做) 这只能部分解决我的困境。但是我在下面的代码中采用了这种方法 以下是与我的具体问题相关的代码: K <- 2 # number of equally-sized (sub)populations N <- 5 # total number of sampled individuals Hstar <- 5 # total number of haplotypes probs <- rep(1

我的问题来源于我之前提出的一个问题,但没有包括我自己的代码(我最初应该这样做)

这只能部分解决我的困境。但是我在下面的代码中采用了这种方法

以下是与我的具体问题相关的代码:

K <- 2  # number of equally-sized (sub)populations
N <- 5 # total number of sampled individuals
Hstar <- 5 # total number of haplotypes
probs <- rep(1/Hstar, Hstar) # haplotype frequencies
m = 0.1 # migration rate between subpopulations
perms <- 10000 # number of permutations

## Set up container(s) to hold the identity of each individual from each permutation ##

num.specs <- ceiling(N / K)

## Create an ID for each haplotype ##

haps <- 1:Hstar

## Assign individuals (N) to each subpopulation (K) ##

specs <- 1:num.specs

## Generate permutations, assume each permutation has N individuals, and sample those individuals' haplotypes from the probabilities ##

gen.perms <- function() {
    sample(haps, size = num.specs, replace = TRUE, prob = probs)
}

pop <- array(dim = c(perms, num.specs, K))

for (i in 1:K) {
    pop[,, i] <- replicate(perms, gen.perms())
}

## Allow individuals from permutations to migrate between subpopulations ##

for (i in 1:K) {
    if (m != 0){
        ind <- sample(perms, size = perms * m, replace = FALSE) # sample random row from random subpopulation
    }
    pop[ind,] ## should swap rows between subarrays, but instead throws an error.
}

K我想你忘了加逗号。我想您需要将第38行更改为:

pop[ind,,] ## should swap rows between subarrays, but instead throws an error.
当您想继续使用已更改的
pop
-array,以及已交换的行时,您需要将其存储在一个变量中。例如:

pop_new <- pop[ind,,]

是的,但我的困惑来自这样一个事实,即更新后的“pop”不再具有与初始“pop”相同的维度。它应该做的只是交换行,而不影响其他任何事情。由于m=0.1且perms=10000,这意味着应随机交换1000行。更新后的“pop”的尺寸为1000 x 3 x 2,而不是10000 x 5 x 2,这是我正在面临并试图解决的问题。您能否更详细地描述一下您想要什么,因为我们创建了一个包含1000行的新阵列。因为在
ind
中有1.000个值…在它是10.000之前,这意味着R将其他值放在其他维度中。好的,我想我更了解你想要什么…看看我在我答案的最后几行中所做的更改。我想它现在对我有效了!非常感谢你的帮助!美好的这样你就可以把问题标记为已解决了。并考虑表决我的答案(;
pop[ind,,] <- pop[sample(ind),,]