Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert list to a matrix or array?

Tags:

r

  > str(mylist)
         List of 50
      $ : logi [1:14] 0.2 0.3 0.2 ...
      $ : logi [1:14] 0.1 0.3 0.6 ...
      $ : logi [1:14] 0.2 0.9 0.1 ...

I want to convert (or write out) mylist to 14 matrices with dim 5 10 or one array with dim 5 10 14

Example data:

mylist <- lapply(1:50, function(i) sample((1:14)/10, 14, repl=TRUE))
like image 860
bic ton Avatar asked May 25 '16 09:05

bic ton


People also ask

How do you convert a list into a matrix?

To convert R List to Matrix, use the matrix() function and pass the unlist(list) as an argument. The unlist() method in R simplifies it to produce a vector that contains all the atomic components which occur in list data.

Can you convert a list to an array?

The best and easiest way to convert a List into an Array in Java is to use the . toArray() method. Likewise, we can convert back a List to Array using the Arrays. asList() method.

What is the difference between a matrix and a list?

The key difference between tables and matrices is that tables can include only row groups, where as matrices have row groups and column groups. Lists are a little different. They support a free-layout that and can include multiple peer tables or matrices, each using data from a different dataset.


1 Answers

This will produce the desired array (with NAs for the nonnumeric values):

array(as.numeric(unlist(mylist)), dim=c(14, 5, 10))
like image 161
jogo Avatar answered Sep 17 '22 18:09

jogo