Best practice on working with lists for matrix/vector multiplications

rstudio
listcolumn
matrix

#1

I currently have two lists with 252 elements. I work with matrix multiplications and want to create outputs for each of the elements in the lists through own, custom functions.

What is the best way to handle this task? Is it simply possible work with the lists as they are, or is it better to convert them to matrix form?


#2

One of the better ways to work with lists at the moment is purrr. It has multiple specialized functions that can make your life easier. For example, purrr::map2 can help you to go through both lists one by one and create another list which is a result of a function.


#3

Is it generally better to work with matrices than lists? The object size is large (15mb) in my case.


#4

Can your lists be easily converted to matrices? What is their structure? What were the reasons they wound up as lists and not matrices up until now?

If you have matrix-appropriate data on which you are doing matrix multiplications, I can’t see a reason not to use matrices, but then I don’t know very much about what you’re doing. Is there a downside that you can see to switching to matrices?

I personally think broad statements about performance aren’t very convincing (the details matter), but the common advice is that the performance of matrices and the built-in matrix manipulation functions will be superior to trying to recreate the same thing using another data structure.


#5

@jcblum

I've used array(as.matrix(unlist())) combination to convert them to matrices. However, I am not sure if this is a correct way to do it (please tell me if there is a better way). I mainly have two lists: the first one has 251 elements where each element is a 80 x 80 "matrix", second list has 251 elements where each element is a 3 x 80 matrix. The reason why they wound up as lists, and not matrices, is because I didn't know of a better way to create the list (or matrix), as it was the only way I could structure the data as I liked.

I do not think there is a downside of switching to matrices, because I am going to work with the matrices by using a custom function with matrix and vector multiplications.