将“dimnames”分配给列表中的所有矩阵时出错

发布于 2025-01-16 19:03:39 字数 1246 浏览 1 评论 0原文

我在这个线程中使用了解决方案 将暗名称分配给矩阵列表在 R 中,但我没有得到我需要的东西。

我有一个包含 4 个矩阵的列表,我想将它们的列和行命名为相同的名称。

A = list(a = matrix(1:4, 2), b = matrix(2:5, 2))

G = list(a = matrix(10:13, 2), b = matrix(5:8, 2))

M_1 = list(a = matrix(10:13, 2), b = matrix(5:8, 2))

M_2 = list(a = matrix(2:5, 2), b = matrix(5:8, 2))

dlist4 <- tibble::lst(A, G, M_1, M_2)
Map(function(x) {dimnames(x) <- list(c("seed","plant"), c("seed","plant")); x}, dlist4)

lapply(dlist4, function(x) {dimnames(x) = list(c("seed","plant"), c("seed","plant")); x})

返回相同的错误: dimnames(x) <- list(c("seed", "plant"), c("seed", "plant")) 中的错误: 'dimnames' 应用于非array

我在这里尝试了循环 R:更改列表中每个矩阵的列名称,也一样,但没有用。

for(i in seq_along(dlist4)) {
        dimnames(dlist4[[i]]) <- list(c("seed","plant"), c("seed","plant"))
 }

dimnames(dlist4[[i]]) <- list(c("seed", "plant"), c("seed", "plant")) 中的错误:'dimnames' 应用于非数组

I used the solution in this thread assign dimnames to a list of matrices in R but I didn't get what I need.

I have a list of 4 matrices that I wanted to name their columns and rows the same.

A = list(a = matrix(1:4, 2), b = matrix(2:5, 2))

G = list(a = matrix(10:13, 2), b = matrix(5:8, 2))

M_1 = list(a = matrix(10:13, 2), b = matrix(5:8, 2))

M_2 = list(a = matrix(2:5, 2), b = matrix(5:8, 2))

dlist4 <- tibble::lst(A, G, M_1, M_2)
Map(function(x) {dimnames(x) <- list(c("seed","plant"), c("seed","plant")); x}, dlist4)

and

lapply(dlist4, function(x) {dimnames(x) = list(c("seed","plant"), c("seed","plant")); x})

returned the same error: Error in dimnames(x) <- list(c("seed", "plant"), c("seed", "plant")) : 'dimnames' applied to non-array

I tried the loop here R: change column names of every matrix in a list, too but it didn't work.

for(i in seq_along(dlist4)) {
        dimnames(dlist4[[i]]) <- list(c("seed","plant"), c("seed","plant"))
 }

Error in dimnames(dlist4[[i]]) <- list(c("seed", "plant"), c("seed", "plant")) : 'dimnames' applied to non-array

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

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

发布评论

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

评论(2

耳根太软 2025-01-23 19:03:39

您的主列表中没有矩阵,您有另一组列表 - 请参阅lapply(dlist4, class)。所以它不是 4 个矩阵,而是 4 组两个矩阵,每个矩阵都在一个列表中。

在这种情况下,您需要递归地向下直到停止点击列表,这意味着 rapply 可能会很方便:

rapply(dlist4, function(x) {dimnames(x) <- rep(list(c("seed","plant")), 2); x}, how="list")
#$A
#$A$a
#      seed plant
#seed     1     3
#plant    2     4
#
#$A$b
#      seed plant
#seed     2     4
#plant    3     5
#
#
#$G
#$G$a
#      seed plant
#seed    10    12
#plant   11    13
#...

You don't have a matrix inside your main list, you have another set of lists - see lapply(dlist4, class). So it's not 4 matrices, it's 4 sets of two matrices, each in a list.

In this instance, you need to recursively go down until you stop hitting lists, which means rapply might be handy:

rapply(dlist4, function(x) {dimnames(x) <- rep(list(c("seed","plant")), 2); x}, how="list")
#$A
#$A$a
#      seed plant
#seed     1     3
#plant    2     4
#
#$A$b
#      seed plant
#seed     2     4
#plant    3     5
#
#
#$G
#$G$a
#      seed plant
#seed    10    12
#plant   11    13
#...
口干舌燥 2025-01-23 19:03:39

由于您没有矩阵列表,而是矩阵列表列表,因此您必须使用嵌套 lapply() 或嵌套循环:

dlist4 <- tibble::lst(A, G, M_1, M_2)

lapply(dlist4, function(x) {lapply(x, `dimnames<-`, rep(list(c("seed", "plant")),2))})
#> $A
#> $A$a
#>       seed plant
#> seed     1     3
#> plant    2     4
#> 
#> $A$b
#>       seed plant
#> seed     2     4
#> plant    3     5
#> 
#> 
#> $G
#> $G$a
#>       seed plant
#> seed    10    12
#> plant   11    13
#> 
#> $G$b
#>       seed plant
#> seed     5     7
#> plant    6     8
#> 
#> 
#> $M_1
#> $M_1$a
#>       seed plant
#> seed    10    12
#> plant   11    13
#> 
#> $M_1$b
#>       seed plant
#> seed     5     7
#> plant    6     8
#> 
#> 
#> $M_2
#> $M_2$a
#>       seed plant
#> seed     2     4
#> plant    3     5
#> 
#> $M_2$b
#>       seed plant
#> seed     5     7
#> plant    6     8

或者,您也可以使用 purrr::map_depth() 专为嵌套列表而设计:

purrr::map_depth(dlist4, 
                 2, 
                 `dimnames<-`, 
                 rep(list(c("seed", "plant")),2)
                 )

reprex 包 (v2.0.1)

Since you don't have a list of matrices, but a list of lists of matrices, you will have to use a nested lapply() or a nested loop:

dlist4 <- tibble::lst(A, G, M_1, M_2)

lapply(dlist4, function(x) {lapply(x, `dimnames<-`, rep(list(c("seed", "plant")),2))})
#> $A
#> $A$a
#>       seed plant
#> seed     1     3
#> plant    2     4
#> 
#> $A$b
#>       seed plant
#> seed     2     4
#> plant    3     5
#> 
#> 
#> $G
#> $G$a
#>       seed plant
#> seed    10    12
#> plant   11    13
#> 
#> $G$b
#>       seed plant
#> seed     5     7
#> plant    6     8
#> 
#> 
#> $M_1
#> $M_1$a
#>       seed plant
#> seed    10    12
#> plant   11    13
#> 
#> $M_1$b
#>       seed plant
#> seed     5     7
#> plant    6     8
#> 
#> 
#> $M_2
#> $M_2$a
#>       seed plant
#> seed     2     4
#> plant    3     5
#> 
#> $M_2$b
#>       seed plant
#> seed     5     7
#> plant    6     8

Alternatively, you could also use purrr::map_depth() which is design specifically for nested lists:

purrr::map_depth(dlist4, 
                 2, 
                 `dimnames<-`, 
                 rep(list(c("seed", "plant")),2)
                 )

Created on 2022-03-25 by the reprex package (v2.0.1)

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