可以从类似于下面的列表中随机抽取一列。
list(combinations(4,3,1:4),combinations(4,2,1:4))
[[1]]
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    4
[3,]    1    3    4
[4,]    2    3    4
[[2]]
    [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    1    4
[4,]    2    3
[5,]    2    4
[6,]    3    4我试图将列组合成一个一维矩阵的列表,例如。list(矩阵,1,矩阵,2...),但也有问题。因为矩阵有不同的维度,所以我不太确定该怎么做。
发布于 2011-10-01 07:02:59
这里有一种方法,首先将矩阵展平为列的列表:
> library(gtools)
> matrices <- list(combinations(4,3,1:4),combinations(4,2,1:4))
> columns <- do.call(c, lapply(matrices, function(x) as.list(as.data.frame(x))))
> columns
$V1
[1] 1 1 1 2
$V2
[1] 2 2 3 3
$V3
[1] 3 4 4 4
$V1
[1] 1 1 1 2 2 3
$V2
[1] 2 3 4 3 4 4
> columns[[sample(length(columns), 1)]]
[1] 1 1 1 2 2 3
> columns[[sample(length(columns), 1)]]
[1] 2 3 4 3 4 4
> columns[[sample(length(columns), 1)]]
[1] 2 2 3 3发布于 2011-10-01 06:52:24
您可以通过一个简单的lapply、as.vector和do.call应用程序来实现这一点
l <- list(matrix(1:16,4,4),matrix(1:8,4,2),matrix(1:20,4,5))                        
rs <-lapply(l,as.vector)
matrix(do.call(c,rs),ncol = 1)但这是我最后一次尝试了。如果你再改一次问题,我就把它删掉然后继续。;)
https://stackoverflow.com/questions/7616794
复制相似问题