Thursday 3 January 2019

r - How to extract matrix object from list without being a list anymore?

You could use x[['b']] or x$b


x = list(a = matrix(sample(1:5,4) , nrow=2, ncol=2),
b = matrix(sample(5:10,4) , nrow=2, ncol=2),
c = matrix(sample(10:15,4) , nrow=2, ncol=2))
x[['b']]
[,1] [,2]
[1,] 6 10
[2,] 9 7
x$b
[,1] [,2]
[1,] 6 10
[2,] 9 7

Here is the microbenchmark between the 2 solutions :


microbenchmark(x[['b']],x$b)
Unit: nanoseconds
expr min lq mean median uq max neval cld
x[["b"]] 351 701 756.80 701 701 3851 100 a
x$b 700 701 942.33 701 1050 15400 100 a

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...