R:如何将矩阵每行中的条目加倍并插入新行

发布于 2025-01-18 20:47:38 字数 707 浏览 0 评论 0原文

mylist <- list(matrix(c(1, 3, -1, 0, 2, 1), nrow = 2, byrow = TRUE),
               matrix(c(-2, 0, 10, 1, 2, 9, 2, 0, 0), nrow = 3, byrow = TRUE))
> mylist
[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    0    2    1

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]    1    2    9
[3,]    2    0    0

我有一个称为myList的矩阵列表,其中矩阵的尺寸可能有所不同。对于每个矩阵,我想将行值加倍并将其插入下面的新行。我所需的输出如下:

[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    2    6   -2
[3,]    0    2    1
[4,]    0    4    2

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]   -4    0   20
[3,]    1    2    9
[4,]    2    4   18
[5,]    2    0    0
[6,]    4    0    0
mylist <- list(matrix(c(1, 3, -1, 0, 2, 1), nrow = 2, byrow = TRUE),
               matrix(c(-2, 0, 10, 1, 2, 9, 2, 0, 0), nrow = 3, byrow = TRUE))
> mylist
[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    0    2    1

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]    1    2    9
[3,]    2    0    0

I have a list of matrices called mylist where the dimensions of the matrices can differ. For each matrix, I want to double the row values and insert it as a new row underneath. My desired output is as follows:

[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    2    6   -2
[3,]    0    2    1
[4,]    0    4    2

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]   -4    0   20
[3,]    1    2    9
[4,]    2    4   18
[5,]    2    0    0
[6,]    4    0    0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

超可爱的懒熊 2025-01-25 20:47:40

lapply 中,rep吃掉使用 seq_lennrow 找到的每一行索引,并重复乘以 1: 2 利用回收利用。

lapply(mylist, \(x) x[rep(seq_len(nrow(x)), each=2), ]*1:2)
# [[1]]
#      [,1] [,2] [,3]
# [1,]    1    3   -1
# [2,]    2    6   -2
# [3,]    0    2    1
# [4,]    0    4    2
# 
# [[2]]
#      [,1] [,2] [,3]
# [1,]   -2    0   10
# [2,]   -4    0   20
# [3,]    1    2    9
# [4,]    2    4   18
# [5,]    2    0    0
# [6,]    4    0    0

In an lapply repeat each row index found using seq_len with nrow and multiply repeatedly by 1:2 exploiting recycling.

lapply(mylist, \(x) x[rep(seq_len(nrow(x)), each=2), ]*1:2)
# [[1]]
#      [,1] [,2] [,3]
# [1,]    1    3   -1
# [2,]    2    6   -2
# [3,]    0    2    1
# [4,]    0    4    2
# 
# [[2]]
#      [,1] [,2] [,3]
# [1,]   -2    0   10
# [2,]   -4    0   20
# [3,]    1    2    9
# [4,]    2    4   18
# [5,]    2    0    0
# [6,]    4    0    0
第七度阳光i 2025-01-25 20:47:40

您可以使用rbind,但是您需要输入行才能在下面乘以乘以:

lapply(
  mylist,
  function(x){
    rbind(x, x * 2)[as.vector(t(matrix(seq_len(nrow(x) * 2), ncol = 2))),]
  }     
)

[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    2    6   -2
[3,]    0    2    1
[4,]    0    4    2

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]   -4    0   20
[3,]    1    2    9
[4,]    2    4   18
[5,]    2    0    0
[6,]    4    0    0

You can use rbind but you need to permute rows to get multiplied row beneath:

lapply(
  mylist,
  function(x){
    rbind(x, x * 2)[as.vector(t(matrix(seq_len(nrow(x) * 2), ncol = 2))),]
  }     
)

[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    2    6   -2
[3,]    0    2    1
[4,]    0    4    2

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]   -4    0   20
[3,]    1    2    9
[4,]    2    4   18
[5,]    2    0    0
[6,]    4    0    0
甜心 2025-01-25 20:47:40

另一个基本R方法也使用rbind do.call 。

lapply(mylist, function(x) 
  do.call(rbind, lapply(1:nrow(x), function(i) rbind(x[i, ], x[i,]*2))))

[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    2    6   -2
[3,]    0    2    1
[4,]    0    4    2

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]   -4    0   20
[3,]    1    2    9
[4,]    2    4   18
[5,]    2    0    0
[6,]    4    0    0

Another base R approach which also utilises the rbind function with do.call.

lapply(mylist, function(x) 
  do.call(rbind, lapply(1:nrow(x), function(i) rbind(x[i, ], x[i,]*2))))

[[1]]
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    2    6   -2
[3,]    0    2    1
[4,]    0    4    2

[[2]]
     [,1] [,2] [,3]
[1,]   -2    0   10
[2,]   -4    0   20
[3,]    1    2    9
[4,]    2    4   18
[5,]    2    0    0
[6,]    4    0    0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文