I have matrix, but want to extend it with the same pattern. Note that it may be extended for any given number of rows and columns, and is not normally square
04/06/2012 11/06/2012 18/06/2012 25/06/2012 02/07/2012
26/03/2012 10 11 12 13 14
02/04/2012 9 10 11 12 13
09/04/2012 8 9 10 11 12
16/04/2012 7 8 9 10 11
23/04/2012 6 7 8 9 10
30/04/2012 5 6 7 8 9
07/05/2012 4 5 6 7 8
14/05/2012 3 4 5 6 7
21/05/2012 2 3 4 5 6
28/05/2012 1 2 3 4 5
Ie I want to extend it to something like this:
04/06/2012 11/06/2012 18/06/2012 25/06/2012 02/07/2012
26/03/2012 10 11 12 13 14
02/04/2012 9 10 11 12 13
09/04/2012 8 9 10 11 12
16/04/2012 7 8 9 10 11
23/04/2012 6 7 8 9 10
30/04/2012 5 6 7 8 9
07/05/2012 4 5 6 7 8
14/05/2012 3 4 5 6 7
21/05/2012 2 3 4 5 6
28/05/2012 1 2 3 4 5
04/06/2012 0 1 2 3 4
11/06/2012 NA 0 1 2 3
18/06/2012 NA NA 0 1 2
25/06/2012 NA NA NA 0 1
02/07/2012 NA NA NA NA 0
I'm sure there's a clever way to do this with Reduce
or something, but this is what came to mind:
lengthOut <- 6 ## Set to one less than the number of columns you want to create
startAt <- 10 ## Set the maximum value of the FIRST column
vapply(c(0, sequence(lengthOut)), function(x) {
x <- (startAt + x):0 # Create a sequence in the normal manner
length(x) <- startAt + lengthOut + 1 # Extend the length of that sequence
x
}, numeric(startAt + lengthOut + 1)) # Specify what to return
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] 10 11 12 13 14 15 16
# [2,] 9 10 11 12 13 14 15
# [3,] 8 9 10 11 12 13 14
# [4,] 7 8 9 10 11 12 13
# [5,] 6 7 8 9 10 11 12
# [6,] 5 6 7 8 9 10 11
# [7,] 4 5 6 7 8 9 10
# [8,] 3 4 5 6 7 8 9
# [9,] 2 3 4 5 6 7 8
# [10,] 1 2 3 4 5 6 7
# [11,] 0 1 2 3 4 5 6
# [12,] NA 0 1 2 3 4 5
# [13,] NA NA 0 1 2 3 4
# [14,] NA NA NA 0 1 2 3
# [15,] NA NA NA NA 0 1 2
# [16,] NA NA NA NA NA 0 1
# [17,] NA NA NA NA NA NA 0
Here's another approach
x <- 16:0
matrix(c(sapply(6:1, function(z) rep(lead(x, z))), x), ncol=7)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,] 10 11 12 13 14 15 16
#[2,] 9 10 11 12 13 14 15
#[3,] 8 9 10 11 12 13 14
#[4,] 7 8 9 10 11 12 13
#[5,] 6 7 8 9 10 11 12
#[6,] 5 6 7 8 9 10 11
#[7,] 4 5 6 7 8 9 10
#[8,] 3 4 5 6 7 8 9
#[9,] 2 3 4 5 6 7 8
#[10,] 1 2 3 4 5 6 7
#[11,] 0 1 2 3 4 5 6
#[12,] NA 0 1 2 3 4 5
#[13,] NA NA 0 1 2 3 4
#[14,] NA NA NA 0 1 2 3
#[15,] NA NA NA NA 0 1 2
#[16,] NA NA NA NA NA 0 1
#[17,] NA NA NA NA NA NA 0
Edit: forgot to mention that I used dplyr::lead
Not sure if this helps:
m1 <- matrix(rep(10:1,each=7)+0:6,ncol=7,byrow=T)
m2 <- matrix(NA,ncol=7,nrow=7)
indx <- 0:6+rep(c(0:-6),each=7)
m2[lower.tri(m2, diag=TRUE)] <- indx[indx>=0]
rbind(m1,t(m2))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] 10 11 12 13 14 15 16
# [2,] 9 10 11 12 13 14 15
# [3,] 8 9 10 11 12 13 14
# [4,] 7 8 9 10 11 12 13
# [5,] 6 7 8 9 10 11 12
# [6,] 5 6 7 8 9 10 11
# [7,] 4 5 6 7 8 9 10
# [8,] 3 4 5 6 7 8 9
# [9,] 2 3 4 5 6 7 8
# [10,] 1 2 3 4 5 6 7
# [11,] 0 1 2 3 4 5 6
# [12,] NA 0 1 2 3 4 5
# [13,] NA NA 0 1 2 3 4
# [14,] NA NA NA 0 1 2 3
# [15,] NA NA NA NA 0 1 2
# [16,] NA NA NA NA NA 0 1
# [17,] NA NA NA NA NA NA 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.