简体   繁体   English

如何在R中绑定大型矩阵并保留格式和属性?

[英]How to rbind large matrices and keep format and attributes in R?

I have matrices like this one z - its how it looks in RStudio env.: 我有这样的矩阵z它在RStudio env中的外观:

z            Large Matrix (625562 elements)
     attr(*, "dimnames")=List of 2
     ..$ : NULL
     ..$ : chr [1:3] "x" "y" "tif"

Now having more like this I try to combine the into one. 现在有更多这样的内容,我尝试将其合并为一个。 I thought this would easy work 我以为这样会很容易

z0 = rbind(z0, z)

and it looks ok in plot but the format is wrong, like: 并且在绘图中看起来还可以,但格式错误,例如:

z0            Large Matrix (19530191 elements)
   : num 414640
   : num 414922
...

This cause problems with further functions. 这会导致其他功能出现问题。 I tried to set attributes like dimnames(z0) <- list(NULL, c("x", "y", "tif")) and in fact the list itself is like that one from z but it doesn't apply. 我试图设置属性,如dimnames(z0) <- list(NULL, c("x", "y", "tif")) ,实际上列表本身就像z那个一样,但它并不适用。 Please how to do it? 请怎么做?

data structures: 数据结构:

> head(z0)
     x        y        tif
[1,] 454647.5 364856.5 0  
[2,] 454657.5 364856.5 0  
[3,] 454658.5 364856.5 0  
[4,] 454635.5 364855.5 0  
[5,] 454656.5 364855.5 0  
[6,] 454657.5 364855.5 0  

> head(z)
            x        y tif
[1,] 456170.5 361799.5   0
[2,] 456171.5 361799.5   0
[3,] 456181.5 361799.5   0
[4,] 456182.5 361799.5   0
[5,] 456192.5 361799.5   0
[6,] 456193.5 361799.5   0

I found what's wrong: in the loop initiator if the object shouldn't be a list. 我发现了问题所在:在循环启动器中,如果对象不应该是列表。

z0 = list()
for (i in 1:K) {
   z0 = rbind(z0, z[i])
}

but z0 = NULL Simple as that! 但是z0 = NULL就这么简单!

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

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