简体   繁体   English

从列表中存储的多个矩阵创建矩阵(r)

[英]Creating a Matrix from Multiple Matrices Stored in a List (r)

Edit: example data added at the bottom. 编辑:示例数据添加在底部。

I want to combine multiple matrices that I have stored in a hierarchical list. 我想合并存储在层次结构列表中的多个矩阵。 The hierarchical list looks something like this: 分层列表如下所示:

Bodies of Water - Sea   - Sea vs Sea   - DataFrame
                                       - Matrix

                        - Sea vs Lake  - DataFrame
                                       - Matrix

                        - Sea vs Pond  - DataFrame
                                       - Matrix

                 - Lake - Lake vs Sea  - DataFrame
                                       - Matrix

                        - Lake vs Lake - DataFrame
                                       - Matrix

                               ...


                        - Pond vs Pond - DataFrame
                                       - Matrix

Ultimately I want to combine the matrices so that they look like this, whereby the names represent a matrix of numbers, pertaining to the parent folder from which they have come. 最终,我想组合矩阵,使它们看起来像这样,其中名称代表数字矩阵,与它们来自的父文件夹有关。 I am of course not striving for a literal table of strings. 我当然不是在争取字面量的字符串表。

   Sea vs Sea   | Sea vs Lake  | Sea vs Pond
----------------|--------------|--------------
   Lake vs Sea  | Lake vs Lake | Lake vs Pond
----------------|--------------|--------------
   Pond vs Sea  | Pond vs Lake | Pond vs Pond

However, I don't know how to do this. 但是,我不知道该怎么做。 As they are in order, I thought a good place to start would be to create a list or array of matrices: 由于它们是按顺序排列的,因此我认为一个不错的起点是创建矩阵列表或数组:

AllMtx <- 'Bodies of Water'$[[i]]"[["Matrix"]]

or 要么

AllMtx <- list.search('Bodies of Water'$[[i]], all(. == Matrix)

Neither of which are even remotely close to working, which isn't helped by the fact I'm struggling to get list.search to return complete lists, not just store every value from objects named "Matrix" in it's own element in a new list. 我都在努力获取list.search以返回完整列表,而不仅仅是将名为“ Matrix”的对象中的每个值都存储在其自己的新元素中,这两者都不至于无法正常工作。名单。

And then I was thinking I could use this list or array or whatever I made, combine the rbind and cbind commands, and produce a final matrix, eg 然后我想我可以使用此列表或数组或其他任何东西,结合rbindcbind命令,并生成最终矩阵,例如

cbind(
        (rbind(AllMtx, 1:3)),
        (rbind(AllMtx, 4:6)),
        (rbind(AllMtx, 7:9))
    )

I was hoping this might sort of resemble a sensible way of assembling the matrix, by first clumping '... vs Sea' , '... vs Lake ' and '... vs Pond' into rows, before finally marrying them all together to fit three columns. 我希望通过将'... vs Sea''... vs Lake '和'... vs Pond'排成一排,最后将它们全部嫁接起来,这有点像是一种明智的组装矩阵的方式一起容纳三列。

Of course I don't really mind how I get my final matrix, but explaining what I'm trying to do often draws useful feedback. 当然,我并不介意如何获得最终矩阵,但是解释自己要做什么通常会得出有用的反馈。

example matrices: 矩阵示例:

list(AvsA = structure(c(1, 4, 5, 6, 1, 7, 4, 8, 1), .Dim = c(3L, 
3L)), AvsB = structure(c(3, 3, 4, 5, 5, 4, 3, 9, 2), .Dim = c(3L, 
3L)), BvsA = structure(c(2, 9, 3, 4, 5, 5, 4, 3, 3), .Dim = c(3L, 
3L)), BvsB = structure(c(1, 3, 4, 5, 1, 4, 3, 9, 1), .Dim = c(3L, 
3L)))


    "V1" "V2" "V3"          "V1" "V2" "V3"     
"1"  1    6    4        "1"  3    5    3    
"2"  4    1    8        "2"  3    5    9      
"3"  5    7    1        "3"  4    4    2      

    "V1" "V2" "V3"          "V1" "V2" "V3"
"1"  2    4    4        "1"  1    5    3
"2"  9    5    3        "2"  3    1    9
"3"  3    5    3        "3"  4    4    1

example of final matrix: 最终矩阵的示例:

structure(c(1, 4, 5, 3, 3, 4, 6, 1, 7, 5, 5, 4, 4, 8, 1, 3, 9, 
2, 2, 9, 3, 1, 3, 4, 4, 5, 5, 5, 1, 4, 4, 3, 3, 3, 9, 1), .Dim = c(6L, 
6L))

    "V1" "V2" "V3" "V4" "V5" "V6"
"1"  1    6    4    2    4    4
"2"  4    1    8    9    5    3
"3"  5    7    1    3    5    3
"4"  3    5    3    1    5    3
"5"  3    5    9    3    1    9
"6"  4    4    2    4    4    1

In order to get the above, a combination of rbind and cbind appeared to work. 为了获得上述效果, rbindcbind的组合似乎起作用了。 However I am still struggling to consolidate all of my small "matrix" objects into a list. 但是,我仍在努力将所有小的“矩阵”对象合并到一个列表中。

This should give you an object to then apply proper rwonames and colnames to: 这应该给您一个对象,然后将适当的rwonames和colname应用于:

with(matL, rbind( cbind(AvsA, AvsB), cbind(BvsA,BvsB)))
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    6    4    3    5    3
[2,]    4    1    8    3    5    9
[3,]    5    7    1    4    4    2
[4,]    2    4    4    1    5    3
[5,]    9    5    3    3    1    9
[6,]    3    5    3    4    4    1

If this were a much larger structure then you might need to refer to the list elements with numeric indices in which case yoyu might choose to use do.call with cbind and rbind: 如果这是一个更大的结构,那么您可能需要使用数字索引来引用列表元素,在这种情况下,yoyu可能会选择对cbind和rbind使用do.call:

with(matL, do.call( rbind, 
                     list( do.call( cbind, matL[1:2]), 
                           do.call( cbind, matL[3:4] ))
                  )
     )
 #------------------------
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    6    4    3    5    3
[2,]    4    1    8    3    5    9
[3,]    5    7    1    4    4    2
[4,]    2    4    4    1    5    3
[5,]    9    5    3    3    1    9
[6,]    3    5    3    4    4    1

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

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