If we have a named array, say a 2-by-3 matrix
amatrix <- cbind(a=1:2, b=3:4, c=5:6)
## a b c
## [1,] 1 3 5
## [2,] 2 4 6
we can subset a column, say #2, by name or by index:
amatrix[, 'b']
## [1] 3 4
amatrix[, 2]
## [1] 3 4
Which of these two subsetting methods is faster, and by how much? I suspect that name subsetting should be slower, owing to string-matching, and wonder if I should take this into account when subsetting hundreds of thousands of arrays.
One question and its answer interestingly report and explain why subsetting lists by [[
can be faster than by $
and vice versa depending on the context. But I have not found any information regarding the present question about [
.