Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unlist a list of matrix elements and get combination of matrix as output instead of as vector in R [duplicate]

Tags:

list

r

If I have a list called List A:

[[1]]
  [1] 11.000000  1.500000  2.666667  2.833333 10.000000  1.500000

[[2]]
  [1]  1.50  1.00  3.25  3.75 10.50  1.50

[[3]]
  [1] 10.5  1.5  1.5  1.5  5.0  5.5

How can I unlist List A in R and get the output as per below:

         [,1]       [,2]      [,3]      [,4]     [,5]      [,6]
  [,1] 11.000000  1.500000  2.666667  2.833333 10.000000  1.500000
  [,2] 1.50       1.00      3.25      3.75     10.50      1.50
  [,3] 10.5       1.5       1.5       1.5      5.0        5.5

If I simply use unlist() it returns a vector

like image 788
DataMiningStudent Avatar asked Nov 25 '25 00:11

DataMiningStudent


2 Answers

We can use do.call with rbind

do.call(rbind, A)
like image 83
akrun Avatar answered Nov 27 '25 14:11

akrun


akrun's answer works well. An alternative method, which utilizes unlist(), is:

matrix(unlist(A), ncol=6, byrow=T)

This converts A into a vector, and then reorganizes it into a matrix with 6 columns, with the elements sorted by row.

like image 39
MBorg Avatar answered Nov 27 '25 15:11

MBorg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!