从R中的四个整数列表生成所有可能的排列 [英] Generate all possible permutations from four integer lists in R

查看:53
本文介绍了从R中的四个整数列表生成所有可能的排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(非常)业余编码员和统计学家正在研究R中的问题.

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

我有四个整数列表:A,B,C,D.

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

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

我希望R通过从每个列表中选择一项来生成所有排列(我知道这段代码将永远运行),然后取每个排列的均值.因此,例如[1,100,200,400]-> 175.25.

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.

有什么想法吗?

推荐答案

即使创建与排列一样大的均值向量也将耗尽您的所有内存.您将不得不将其分解为较小的问题,查找要写入Excel的对象,然后从内存中删除对象

Even creating a vector of means as large as your permutations will use up all of your memory. You will have to split this into smaller problems, look up writing objects to excel and then removing objects from memory here (both on SO).

对于执行此操作的代码,我尝试使其尽可能简单,以便轻松地扩展"您的知识:

As for the code to do this, I've tried to keep it as simple as possible so that it's easy to 'grow' your knowledge:

#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) 

这篇关于从R中的四个整数列表生成所有可能的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆