2

(Very) amateur coder and statistician working on a problem in R.

I have four integer lists: A, B, C, D.

A <- [1:133]
B <- [1:266]
C <- [1:266]
D <- [1:133, 267-400]

I want R to generate all of the permutations from picking 1 item from each of these lists (I know this code will take forever to run), and then take the mean of each of those permutations. So, for instance, [1, 100, 200, 400] -> 175.25.

Ideally what I would have at the end is a list of all of these means then.

Any ideas?

4

2 回答 2

1

对于一个较小但类似的问题,我会这样做:

A <- 1:13
B <- 1:26
C <- 1:26
D <- c(1:13, 27:40)

mymat <- expand.grid(A, B, C, D)
names(mymat) <- c("A", "B", "C", "D")
mymat <- as.matrix(mymat)
mymeans <- rowSums(mymat)/4

如果你只设置所有索引,你可能会崩溃 R,但你可能会设置一个循环,像这样(未经测试):

B <- 1:266
C <- 1:266
D <- c(1:133, 267:400)

for(A in 1:133) {
    mymat <- expand.grid(A, B, C, D)
    names(mymat) <- c("A", "B", "C", "D")
    mymat <- as.matrix(mymat)
    mymeans <- rowSums(mymat)/4
    write.table(mymat, file = paste("matrix", A, "txt", sep = "."))
    write.table(mymeans, file = paste("means", A, "txt", sep = "."))
    rm(mymat, mymeans)
}

得到他们所有。那仍然可能太大,在这种情况下你可以做一个嵌套循环,或者循环D(因为它是最大的)

或者,

n <- 1e7
A <- sample(133, size = n, replace= TRUE)
B <- sample(266, size = n, replace= TRUE)
C <- sample(266, size = n, replace= TRUE)
D <- sample(x = c(1:133, 267:400), size = n, replace= TRUE)
mymeans <- (A+B+C+D)/4

将为您提供大量的手段样本,并且完全不需要时间。

hist(mymeans)
于 2012-12-13T00:44:09.437 回答
1

即使创建与排列一样大的均值向量也会耗尽所有内存。您将不得不将其拆分为较小的问题,查找将对象写入 excel,然后在此处从内存中删除对象(均在 SO 上)。

至于执行此操作的代码,我已尝试使其尽可能简单,以便轻松“增长”您的知识:

#this is how to create vectors of sequential integers integers in R
a <- c(1:33)
b <- c(1:33)
c <- c(1:33)
d <- c(1:33,267:300)

#this is how to create an empty vector
means <- rep(NA,length(a)*length(b)*length(c)*length(d))
#set up for a loop
i <- 1

#how you run a loop to perform this operation
for(j in 1:length(a)){
    for(k in 1:length(b)){
        for(l in 1:length(c)){
            for(m in 1:length(d)){
                y <- c(a[j],b[k],c[l],d[m])
                means[i] <- mean(y)
                i <- i+1
            }
        }
    }
}

#and to graph your output
hist(means, col='brown')
#lets put a mean line through the histogram
abline(v=mean(means), col='white', lwd=2) 
于 2012-12-13T00:56:52.473 回答