如何在R中引用递增变量? [英] How to reference incrementing variables in R?

查看:170
本文介绍了如何在R中引用递增变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建200个尺寸为200 X 129的矩阵.我有一些代码需要在200个矩阵上运行,但是每个新矩阵都引用前一个矩阵.

I want to create 200 matrices of dimensions 200 X 129. I have a bit of code that needs to run over the 200 matrices, but each new matrix references the previous one.

for(i in 1:200)
{
  nam <- paste("step", i, sep = "")
  mat<- matrix(ncol=129, nrow=200)
  assign(nam, mat)
  stepg<- matrix(ncol=129, nrow=200)
  stepg<- step[i][200,129]
  index<-sample(1:nrow(stepg), 2)
  }

运行此代码时,出现错误步骤[i] [20、30]中的错误:尺寸数不正确".我想知道如何引用第i个矩阵.

When I run this code, I get an error "Error in step[i][20, 30] : incorrect number of dimensions". I want to know how to reference the ith matrix.

推荐答案

我不太确定这个问题在问什么,但是您可能会发现此概念很有用:而不是创建具有唯一名称的矩阵,例如step1step2,您可以将这些对象作为元素存储在列表中:

I'm not altogether sure what the question is asking, but you might find this concept helpful: instead of creating matrices with unique names such as step1 and step2, you can store these objects as elements in a list:

storage_list <- vector(mode="list", length=200)
for(i in 1:200) {
    storage_list[[i]] <- matrix(...)
}

然后,您可以轻松访问例如storage_list[[i-1]].

Then you can easily access, e.g., storage_list[[i-1]].

这篇关于如何在R中引用递增变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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