简体   繁体   English

R:创建行数未知的矩阵

[英]R: creating a matrix with unknown number of rows

I have written the code below to generate a matrix containing what is, to me, a fairly complex pattern. 我已经编写了下面的代码来生成一个矩阵,其中包含对我来说相当复杂的模式。 In this case I determined that there are 136 rows in the finished matrix by trial and error. 在这种情况下,我通过反复试验确定完成矩阵中有136行。

I could write a function to calculate the number of matrix rows in advance, but the function would be a little complex. 我可以编写一个函数来预先计算矩阵行的数量,但函数会有点复杂。 In this example the number of rows in the matrix = ((4 * 3 + 1) + (3 * 3 + 1) + (2 * 3 + 1) + (1 * 3 + 1)) * 4. 在该示例中,矩阵中的行数=((4 * 3 + 1)+(3 * 3 + 1)+(2 * 3 + 1)+(1 * 3 + 1))* 4。

Is there an easy and efficient way to create matrices in R without hard-wiring the number of rows in the matrix statement? 有没有一种简单有效的方法在R中创建矩阵而不用硬连接矩阵语句中的行数? In other words, is there an easy way to let R simply add a row to a matrix as needed when using for-loops? 换句话说,是否有一种简单的方法让R在使用for循环时根据需要简单地向矩阵添加一行?

I have presented one solution that employs rbind at each pass through the loops, but that seems a little convoluted and I was wondering if there might be a much easier solution. 我提出了一个解决方案,每次通过循环使用rbind,但这似乎有点复杂,我想知道是否有一个更容易的解决方案。

Sorry if this question is redundant with an earlier question. 对不起,如果这个问题与之前的问题有关。 I could not locate a similar question using the search feature on this site or using an internet search engine today, although I think I have found a similar question somewhere in the past. 我无法使用本网站上的搜索功能或今天使用互联网搜索引擎找到类似的问题,尽管我认为我在过去的某处发现过类似的问题。

Below are 2 sets of example code, one using rbind and the other where I used trial and error to set nrow=136 in advance. 下面是2组示例代码,一组使用rbind,另一组使用试验和错误预先设置nrow = 136。

Thanks for any suggestions. 谢谢你的任何建议。

v1     <- 5
v2     <- 2
v3     <- 2
v4     <- (v1-1)

my.matrix <- matrix(0, nrow=136, ncol=(v1+4) )

i = 1

for(a in 1:v2) {
  for(b in 1:v3) {
    for(c in 1:v4) {
      for(d in (c+1):v1) {

        if(d == (c+1)) l.s = 4 
        else           l.s = 3

        for(e in 1:l.s) {

          my.matrix[i,c] = 1

            if(d == (c+1)) my.matrix[i,d]  = (e-1)
            else           my.matrix[i,d]  =  e

          my.matrix[i,(v1+1)] = a
          my.matrix[i,(v1+2)] = b
          my.matrix[i,(v1+3)] = c
          my.matrix[i,(v1+4)] = d

          i <- i + 1

        }
      }
    }
  }
}

my.matrix2 <- matrix(0, nrow=1, ncol=(v1+4) )
my.matrix3 <- matrix(0, nrow=1, ncol=(v1+4) )

i = 1

for(a in 1:v2) {
  for(b in 1:v3) {
    for(c in 1:v4) {
      for(d in (c+1):v1) {

        if(d == (c+1)) l.s = 4 
        else           l.s = 3

        for(e  in 1:l.s) {

          my.matrix2[1,c] = 1

          if(d == (c+1)) my.matrix2[1,d]  = (e-1)
          else           my.matrix2[1,d]  =  e

          my.matrix2[1,(v1+1)] = a
          my.matrix2[1,(v1+2)] = b
          my.matrix2[1,(v1+3)] = c
          my.matrix2[1,(v1+4)] = d

          i <- i+1

          if(i == 2) my.matrix3 <- my.matrix2
          else       my.matrix3 <- rbind(my.matrix3, my.matrix2)

          my.matrix2 <- matrix(0, nrow=1, ncol=(v1+4) )

        }
      }
    }
  }
}

all.equal(my.matrix, my.matrix3)

If you have some upper bound on the size of the matrix, you can create a matrix large enough to hold all the data 如果矩阵的大小有一些上限,则可以创建一个足以容纳所有数据的矩阵

my.matrix <- matrix(0, nrow=v1*v2*v3*v4*4, ncol=(v1+4) )

and truncate it at the end. 并在最后截断它。

my.matrix <- my.matrix[1:(i-1),]

This is the generic form to do it. 这是执行它的通用形式。 You can adapt it to your problem 您可以根据您的问题进行调整

matrix <- NULL
for(...){
 ...
 matrix <- rbind(matriz,vector)
}

where vector contains the row elements vector包含行元素

I stumbled upon this solution today: convert the matrix to a data.frame . 我今天偶然发现了这个解决方案:将matrix转换为data.frame As new rows are needed by the for-loop those rows are automatically added to the data.frame . 由于for-loop需要新行,这些行会自动添加到data.frame Then you can convert the data.frame back to a matrix at the end if you want. 然后,如果需要,可以将data.frame转换回最后的matrix I am not sure whether this constitutes something similar to iterative use of rbind . 我不确定这是否构成类似于迭代使用rbind东西。 Perhaps it becomes very slow with large data.frames . 对于大型data.frames它可能变得非常慢。 I do not know. 我不知道。

my.data <- matrix(0, ncol = 3, nrow = 2)
my.data <- as.data.frame(my.data)

j <- 1

for(i1 in 0:2) {
     for(i2 in 0:2) {
          for(i3 in 0:2) {

                    my.data[j,1] <- i1
                    my.data[j,2] <- i2
                    my.data[j,3] <- i3

                    j <- j + 1

          }
     }
}

my.data
my.data <- as.matrix(my.data)
dim(my.data)
class(my.data)

EDIT: July 27, 2015 编辑:2015年7月27日

You can also delete the first matrix statement, create an empty data.frame then convert the data.frame to a matrix at the end: 您还可以删除第一个matrix语句,创建一个空的data.frame然后将data.frame转换为data.framematrix

my.data <- data.frame(NULL,NULL,NULL)

j <- 1

for(i1 in 0:2) {
     for(i2 in 0:2) {
          for(i3 in 0:2) {

                    my.data[j,1] <- i1
                    my.data[j,2] <- i2
                    my.data[j,3] <- i3

                    j <- j + 1
          }
     }
}

my.data
my.data <- as.matrix(my.data)
dim(my.data)
class(my.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM