Hi, I'm solve a problem where I want to select every 3rd row from a matrix. I do this for all rows in a loop. This gives me "i" matrices with different number of rows
I want to stack these matrices in an array and fill the blanks with zero. Does anyone have any suggestions on how to go about this? So i want to go from [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4 [5,] 5 5 5 [6,] 6 6 6 [7,] 7 7 7 [8,] 8 8 8 [9,] 9 9 9 to , , 1 [,1] [,2] [,3] [1,] 1 1 1 [2,] 4 4 4 [3,] 7 7 7 , , 2 [,1] [,2] [,3] [1,] 2 2 2 [2,] 5 5 5 [3,] 8 8 8 , , 3 [,1] [,2] [,3] [1,] 3 3 3 [2,] 6 6 6 [3,] 9 9 9 , , 4 [,1] [,2] [,3] [1,] 4 4 4 [2,] 7 7 7 [3,] 0 0 0 ... , , 9 [,1] [,2] [,3] [1,] 9 9 9 [2,] 0 0 0 [3,] 0 0 0 Here's my go at it z <- array(c(1:9), c(9,3)) z1 <- array(0, c(3,3,9)) for(i in 1:9){ if(nrow(z[seq(i,9,3),]) == 2) z1[,,i] <- rbind(z[seq(i,9,3),],c(0,0,0)) else z1[,,i] <- z[seq(i,9,3),] } print(z1) I get the following error: Error in if (nrow(z[seq(i, 9, 3), ]) == 2) z1[, , i] <- rbind(z[seq(i, : argument is of length zero This is because nrow(z[seq(i, 9, 3) becomes NULL. Furthermore, this doesn't take care of the case where nrow(z[seq(i,9,3),]) ==1 Sorry for the long email and thank you in advance for reading it ;) Kristian [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.