简体   繁体   English

每两行生成一个升序矩阵,R中没有for循环

[英]Generating a matrix of ascending values, after every 2 rows, without a for loop in R

I'm trying to generate this matrix without using a for loop (to speed up the processing time): 我正在尝试不使用for循环来生成此矩阵(以加快处理时间):

       Module 1 Module 2 Module 3
y0       20       20       20
y1       20       20       20
y0       40       40       40
y1       40       40       40
y0       60       60       60
y1       60       60       60
y0       80       80       80
y1       80       80       80
y0      100      100      100
y1      100      100      100
y0      120      120      120
y1      120      120      120
y0      140      140      140
y1      140      140      140
y0      160      160      160
y1      160      160      160
y0      180      180      180
y1      180      180      180
y0      200      200      200
y1      200      200      200

I have been attempting the apply functions, replicate() and do.call(). 我一直在尝试应用功能,replicate()和do.call()。 I've ended up with this code: 我最终得到了以下代码:

a = 10          # Number of increments
n1 = 100       # Sample size
m.fn <- function(n1, a) {
  b <- matrix(rep(n1), nrow = 2, ncol = 3)
  rownames(b) <- c("y0", "y1")
  c <- do.call(rbind, replicate(a, b, simplify=FALSE))

  colnames(c) <- c("Module 1", "Module 2", "Module 3")
  return(c)
}

Which produces a matrix similar to above, but with all entries as 20. 产生类似于上面的矩阵,但所有条目均为20。

I've tried replacing the values with little success, as per below: 我尝试将值替换为几乎没有成功,如下所示:

 a = 10          # Number of increments
 n1 = 100       # Sample size
    m.fn <- function(n1, a) {
      b <- matrix(rep(n1), nrow = 2, ncol = 3)
      rownames(b) <- c("y0", "y1")
      c <- do.call(rbind, replicate(a, b, simplify=FALSE))
      c[3:(2 * a),] <- (n1 * seq(3, 2 * a))
      colnames(c) <- c("Module 1", "Module 2", "Module 3")
      return(c)
    }

This is what results: 结果如下:

       Module 1 Module 2 Module 3
y0       20       20       20
y1       20       20       20
y0       60       60       60
y1       80       80       80
y0      100      100      100
y1      120      120      120
y0      140      140      140
y1      160      160      160
y0      180      180      180
y1      200      200      200
y0      220      220      220
y1      240      240      240
y0      260      260      260
y1      280      280      280
y0      300      300      300
y1      320      320      320
y0      340      340      340
y1      360      360      360
y0      380      380      380
y1      400      400      400

For reference, this is the code that I used to create my desired matrix, involving a for loop: 作为参考,这是我用来创建所需矩阵的代码,其中涉及一个for循环:

    n.fn <- function(n1, a) {
  b <- matrix(rep(1, 3), nrow = 1, ncol = 3)
  for (no in 1:a) {
    c <- matrix(rep(n1 * no, 6), nrow = 2, ncol = 3)
    rownames(c) <- c("y0", "y1")
    b <- rbind(b, c)
  }
  b <- as.matrix(b[-1, 1:3])
  colnames(b) <- c("Module 1", "Module 2", "Module 3")
  return(b)
}
n <- n.fn(n1, a)

Any help with creating the first matrix, without the use of a for loop, would be gratefully appreciated! 在不使用for循环的情况下,创建第一个矩阵的任何帮助将不胜感激!

You can just generate the sequence from 20 to 400, repeat it as many times as there are columns times and arrange in a matrix: 您可以只生成20到400的序列,将其重复的次数与列时间相同,并排列成矩阵:

 df <- matrix(rep(rep(seq(from = 20, to = 400, by = 20), each = 2), times = 3), ncol = 3)

Another way is per Adam Quek suggestion using replicate: 根据Adam Quek的建议,另一种方法是使用复制:

df <- replicate(3, unlist(lapply(seq(20,200,20), rep, 2) )) 

Now just give it the desired column and row names: 现在,只需为其指定所需的列和行名称即可:

colnames(df) <- paste("Module", 1:ncol(df))
rownames(df) <- rep(paste0("y", 0:1), nrow(df)/2)

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

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