在R中使用for循环命令连接矩阵

时间:2016-04-19 08:24:37

标签: r for-loop

考虑我们生成5个随机矩阵的可重现数据。现在我想组合每个矩阵的第一行中的数据

set.seed(123)

Result <- foreach(i=1:5) %do%{
  # randomly select number of rows and columns  
  random.rows <- sample(1:5, 1)
  random.columns <- sample(1:5, 1)
  # generate matrix out of this
  matrix(sample(1:100, random.rows*random.columns), random.rows)
}

所需的输出应采用以下形式:

c(Result[[1]][1,], Result[[2]][1,], Result[[3]][1,] # ........
  )

我试图在现实生活中实现for-loop,我有超过5个矩阵。有人可以引导我达到我想要的输出

for (i in 1:5) {
  desired_out <- c(Result[[i]][1,])
}

0 个答案:

没有答案
相关问题