FelipeCruzV10
FelipeCruzV10

Reputation: 486

Plotting elements from list of matrices in R

I have a list of 20,000 entries. Each entry is a 25x3 matrix.

I need to generate a plot where x=1,...,20000 and y = list[x][1,3] (this sintaxis doesn't work).

I don't want to iterate through the whole list and save the values in a new vector because I don't want to waste memory if not necessary.

In summary, I need to get the same entry of every matrix that is inside a list, and plot those values.

BETAJ is the list of matrices. I tried the following enter image description here

What I'm expecting to get, is the value at position [1,3] from the matrices at position 1 through 10,000.

Upvotes: 1

Views: 316

Answers (1)

b.morledge-hampton
b.morledge-hampton

Reputation: 95

First off, be careful how you subset your list. list[1] returns a list containing only the first matrix from your larger list. You want to use list[[x]][1,3] to return the matrix itself.

However, passing this as the y-value in the plot() function will not work as x is not defined. To fix this, I would recommend using sapply() to iterate over your list of matrices and retrieve the values you want for plotting. It would look something like this:

plot(1:20000, sapply(1:20000, function(x) list[[x]][1,3]))

I am not quite sure what the memory footprint of using an apply function in-line like this is, but at the very least, it should be less than the footprint of the larger list itself, since it is a very, very small subset of it.

Upvotes: 1

Related Questions