Thursday, January 3, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...