在每个for循环迭代中创建一个新矩阵

时间:2017-03-23 09:08:26

标签: r loops matrix

我想使用for循环在循环的每次迭代中创建一个新矩阵。

mining_t = c("Zr_t","Sr_t","Rb_t","Zn_t","Cu_t","Fe_t","Mn_t","V_t","Ti_t","Ca_t","K_t","Ba_t")

for (i in mining_t){
  ??=matrix(c(VI9A_mn[1:7,i],VI9A_mn[8:14,i],VI9A_mn[15:21,i]),nrow=7,byrow=FALSE)
  colnames(i)=c("45","90","135")
}

我希望每个矩阵有3列7行。每个新矩阵都由矩阵VI9A_mn的第i列(所有行)组成,并且应该具有向量mining_t的关联元素的名称。

1 个答案:

答案 0 :(得分:0)

我们可以按'VI9A_mn'

列进行split
lst <- lapply(split(VI9A_mn, col(VI9A_mn)), 
                matrix, ncol=3, dimnames = list(NULL, c(45, 90, 135)))

最好将matrix个对象保留在list中。但是,如果我们坚持在全球环境中拥有多个对象,请使用list2env

list2env(lst, envir = .GlobalEnv)