-2

So I have a large list of matrices and I want to be able to take them one by one and unlist them into their normal 2D individual matrices. So let's say I have a list of matrices examplelist. I obtained the list with this code:

examplelist <- lapply(listofimages, readImage)

And here is one sample image 512x512 .tif which would be one of the matrices in the list of matrices :

The list looks like this:

Example matrix list

I want to take out one matrix at a time as a 2D 512x512 matrix but when I try to trim out the other data manually its still a list element (single quotes to make the index not referenced):

a <- as.matrix(examplelist['1'])

b <- a[,]

enter image description here

And when I do this it flattens into a 1D vector:

b <- as.numeric(unlist(examplelist['1']))

enter image description here

Which is 262144 x 1 instead of 512 x 512.

Ive tried converting it to a data table, data frame, and a few other things and then when I get to trying to make it numeric:

as.numeric(b)

I get this error:

enter image description here

Using dput, it lists this as the error:

enter image description here

Is there an easy way to unlist a square matrix from a list of matrices and get it in its original 512 x 512 form rather than a 1D vector (as seen above)?

I tried trimming dimensions (seen above) to get rid of some of the other info but it threw an error. I also tried accessing just the data attributes using '@' and '$' but that didn't work either.

If I try this:

b <- as.data.table(examplelist['1'])

It just gives me another 262144 x 1 1D vector instead of the desired 2D matrix.

This solution and this, don't work because I dont want a 1D vector or to only convert elements (I want the whole matrix) as numeric elements.

Community
  • 1
  • 1
SqueakyBeak
  • 366
  • 4
  • 15
  • 1
    Do not post images as it means others need to retype it. Instead, reduce your problem to a minimal one and show your input by showing the output of running `dput` on it. – G. Grothendieck Jun 18 '19 at 13:50
  • Use double brackets: `examplelist[['1']]` instead of `examplelist['1']`. Pass each of those to `class()` and you'll see the first is a matrix, the second is a list. – Dan Jun 18 '19 at 13:54
  • @G.Grothendieck I have provided my code and only added images to show the error codes and output variable types. – SqueakyBeak Jun 18 '19 at 14:00
  • @Lyngbakr the single quotes are only to make stack overflow not reference my element as a picture/link – SqueakyBeak Jun 18 '19 at 14:01
  • 1
    @SqueakyBeak I'm not worried about the quotes, but rather the brackets. If you use one set of brackets `[]` you get a list; if you use two `[[]]` you get the element itself. See [here](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el). – Dan Jun 18 '19 at 14:04
  • @Lyngbakr ahh sorry I didn't realize what you meant. When I do that, it retrieves a 262144 x 1 1D vector version of the 512x512 matrix. The issue is I wanted to get a 2D 512x512 matrix :/ – SqueakyBeak Jun 18 '19 at 14:07
  • 1
    @SqueakyBeak Without a reproducible example (i.e., code + data), it's hard to demonstrate. `dput` is used in conjunction with an object. For example, `dput(examplelist)` would output `examplelist` in a copy-and-pasteable format. I'll post an answer without a reproducible example... – Dan Jun 18 '19 at 14:14
  • The data needs to be provided in reproducible form, not as an image, and it needs to be cut down to a small problem that still illustrates it. – G. Grothendieck Jun 18 '19 at 14:14
  • @G.Grothendieck since the data is an image, it would need to be saved to your local disk. Would you like me to provide an imgur link or something else for the image? A google drive link? – SqueakyBeak Jun 18 '19 at 14:18

1 Answers1

1

To extract an element of a list you need to use double brackets. Consider the example below. Let's create a list of matrices.

# Dummy list
foo <- list(first = matrix(runif(16), ncol = 4),
            second = matrix(runif(16), ncol = 4))

This looks like the following:

# Quick peek
print(foo)
#> $first
#>           [,1]      [,2]       [,3]      [,4]
#> [1,] 0.3517863 0.1222894 0.69358440 0.7850944
#> [2,] 0.7516454 0.9881041 0.72152473 0.3035514
#> [3,] 0.8540138 0.3966431 0.40551019 0.3687717
#> [4,] 0.8872717 0.7438446 0.03258007 0.1305907
#> 
#> $second
#>            [,1]       [,2]       [,3]       [,4]
#> [1,] 0.57426947 0.59617809 0.05355548 0.05962695
#> [2,] 0.60420788 0.06640785 0.43616808 0.03359352
#> [3,] 0.44216820 0.58033207 0.22686284 0.42624557
#> [4,] 0.08838313 0.27258925 0.71353586 0.76606084

Now, let's have a look at the first element using just one set of brackets.

# Extract one element as a list
a <- foo['first'] # Or foo[1]

# Examine output
print(a)
#> $first
#>           [,1]      [,2]       [,3]      [,4]
#> [1,] 0.3517863 0.1222894 0.69358440 0.7850944
#> [2,] 0.7516454 0.9881041 0.72152473 0.3035514
#> [3,] 0.8540138 0.3966431 0.40551019 0.3687717
#> [4,] 0.8872717 0.7438446 0.03258007 0.1305907
class(a)
#> [1] "list"

You'll notice it's a list. Let's try using two sets of brackets.

# Extract one element
b <- foo[['first']] # Or foo[[1]]

# Examine output
print(b)
#>           [,1]      [,2]       [,3]      [,4]
#> [1,] 0.3517863 0.1222894 0.69358440 0.7850944
#> [2,] 0.7516454 0.9881041 0.72152473 0.3035514
#> [3,] 0.8540138 0.3966431 0.40551019 0.3687717
#> [4,] 0.8872717 0.7438446 0.03258007 0.1305907
class(b)
#> [1] "matrix"

Created on 2019-06-18 by the reprex package (v0.3.0)

It's a matrix! Plus, it maintains the original structure of the matrix.

Dan
  • 11,370
  • 4
  • 43
  • 68
  • Thank you, Im wondering if its because when using readImage from EBImage, that I'm getting this error: > b <- as.matrix(examplelist[['1']]) Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : 'data' must be of a vector type, was 'NULL' – SqueakyBeak Jun 18 '19 at 14:24
  • Oh wait, I had to take out the single quotes. Thank you it works! Sorry for the confusion! – SqueakyBeak Jun 18 '19 at 14:26