-2

I would like to get better dimensions of R data structures, especially tensors and list of lists. I think I have now a tensor or four lists of lists passed to my function i.e. four matrices, which causes the bug, which I did not notice because str(Matrix) says only List of 4, better would be dimensions etc 4x1505x1505. Also print(dim(Matrix)) returns NULL because it is designed for matrix/array/data frame. I have the following inside a function

print("Just before the matrix")
print(dim(Matrix))

str(Matrix)

print("Just before turning the matrix")

Matrix <- apply(Matrix, 2, rev) # http://stackoverflow.com/a/9135850/54964

Output

[1] "Just before the matrix"
NULL
List of 4
 $ : num [1:1505, 1:1505] 1 -0.2936 -0.0394 -0.1898 0.4661 ...
 $ : num [1:1505, 1:1505] 1 -0.4002 0.3807 -0.0657 -0.2308 ...
 $ : num [1:1505, 1:1505] 1 0.63871 -0.03072 -0.11423 -0.00918 ...
 $ : num [1:1505, 1:1505] 1 0.1347 -0.0531 -0.0836 -0.0101 ...
[1] "Just before turning the matrix"
Error in apply(Matrix, 2, rev) : dim(X) must have a positive length
Calls: makeMatrixPlot -> apply
Execution halted

Proposal: get dimensions of the data structure than descriptive words

R: 3.3.1
OS: Debian 8.5

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 1
    Since your matrix is actually a list of matrices, you might want to use `lapply(Matrix, function(m) apply(m, 2, rev))` and `lapply(Matrix, dim)` – HubertL Nov 04 '16 at 22:53
  • Can you create simply such a list of matrices minimally? - - It would be very useful. My attempt `a <- c(matrix(rnorm(1505), ncol=1505, nrow=1505), matrix(rnorm(1505), ncol=1505, nrow=1505), matrix(rnorm(1505), ncol=1505, nrow=1505))`. - - Trying to test your proposal with minimal example. – Léo Léopold Hertz 준영 Nov 04 '16 at 23:00
  • use `list()` instead of `c()` – HubertL Nov 04 '16 at 23:06

1 Answers1

1

The dimensions are NOT 4 x 1505 x 1505. The "Matrix" object does not in fact have a dim attribute, so you get NULL reported by dim. It has a length of 4. If you want to construct an array with those dimensions then you need to install the abind library and to load it:

M <- list( matrix(0, 3,3), matrix(1,3,3), matrix(2, 3,3) )
library(abind)
( M333 <-  abind(M, along=3) )
#----------
, , 1

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0

, , 2

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1

, , 3

     [,1] [,2] [,3]
[1,]    2    2    2
[2,]    2    2    2
[3,]    2    2    2

If you sue rev on that object you will need to specify which marginal you are interested in. Read the help page for apply and play around a bit with smaller examples.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • What are the dimensions of the structure `num [1:1505, 1:1505]`? Is it 1505x1505 matrix or 1505x1 row/column? – Léo Léopold Hertz 준영 Nov 05 '16 at 09:58
  • 1
    1) `dim(M333)` 2) Your "Matrix" already is a "big list" (and not a matrix) 3) `num [1:1505, 1:1505]` is the way that `str` presents the structure of an 1505 row by 1505 col matrix. – IRTFM Nov 05 '16 at 17:08
  • That sounds like a completely different question. Above you were starting with a list and asking for an object that had a `dim`-attribute. Now it appears you want a list starting from an R matrix. StackOverflow comments are not supposed to spport a chat room style of interaction. You should be searching on the relevant terms and posting questions when your searches are unsuccessful. – IRTFM Nov 05 '16 at 17:58