I'm trying to generate this matrix without using a for loop (to speed up the processing time):
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(). 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.
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:
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!