简体   繁体   English

通过将行添加到具有相同模式的R中的矩阵进行扩展

[英]Extend by adding rows to a matrix in R with the same pattern

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: 我敢肯定,使用Reduce或其他方法可以做到这一点,但这是我想到的:

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 编辑:忘记提及我使用了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

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

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