Indexing

This document examines different ways to index an variable.

Create dummy vector, matrix, array and list

aVec <- c(1, 2, 3)
names(aVec) <- c("x1", "x2", "x3")

aMat <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, dimnames = list(c("x1", "x2"), 
    c("y1", "y2")))


aArray <- array(c(1, 2, 3, 4, 5, 6, 7, 8), dim = c(2, 2, 2), dimnames = list(c("x1", 
    "x2"), c("y1", "y2"), c("z1", "z2")))

aList <- list(x1 = 1, x2 = 2, y1 = 3)

bList <- list(1, 2)

What do these variables look like?

aVec
## x1 x2 x3 
##  1  2  3
aMat
##    y1 y2
## x1  1  3
## x2  2  4
aArray
## , , z1
## 
##    y1 y2
## x1  1  3
## x2  2  4
## 
## , , z2
## 
##    y1 y2
## x1  5  7
## x2  6  8
aList
## $x1
## [1] 1
## 
## $x2
## [1] 2
## 
## $y1
## [1] 3
bList
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 2

Extract using x[i]

aVec[2]
## x2 
##  2
aMat[2]
## [1] 2
aArray[2]
## [1] 2
aList[2]
## $x2
## [1] 2
bList[2]
## [[1]]
## [1] 2

Extract using x[i,j]

aVec[2, 1]  # wrong
## Error: incorrect number of dimensions
aMat[2, 1]
## [1] 2
aArray[2, 1]  # wrong
## Error: incorrect number of dimensions
aArray[2, 1, 1]
## [1] 2
aList[2, 1]  # wrong
## Error: incorrect number of dimensions
bList[2, 1]  # wrong
## Error: incorrect number of dimensions

Extract using x[[i]]

aVec[[2]]
## [1] 2
aMat[[2]]
## [1] 2
aArray[[2]]
## [1] 2
aList[[2]]
## [1] 2
bList[[2]]
## [1] 2

Extract using x[[i,j]]

aVec[[2, 1]]  # wrong
## Error: incorrect number of subscripts
aMat[[2, 1]]
## [1] 2
aArray[[2, 1]]  # wrong
## Error: incorrect number of subscripts
aArray[[2, 1, 1]]
## [1] 2
aList[[2, 1]]  # wrong
## Error: incorrect number of subscripts
bList[[2, 1]]  # wrong
## Error: incorrect number of subscripts

Extract using x$a

aVec$x1
## Error: $ operator is invalid for atomic vectors
aVec$x
## Error: $ operator is invalid for atomic vectors
aVec$y
## Error: $ operator is invalid for atomic vectors

aMat$x1
## Error: $ operator is invalid for atomic vectors
aMat$x
## Error: $ operator is invalid for atomic vectors
aMat$y
## Error: $ operator is invalid for atomic vectors

aArray$x1
## Error: $ operator is invalid for atomic vectors
aArray$x
## Error: $ operator is invalid for atomic vectors
aArray$y
## Error: $ operator is invalid for atomic vectors

aList$x1
## [1] 1
aList$x  # return NULL, partial matching will not work
## NULL
aList$y  # partial matching
## [1] 3

Extract using x$“a”

aVec$x1
## Error: $ operator is invalid for atomic vectors
aVec$x
## Error: $ operator is invalid for atomic vectors
aVec$y
## Error: $ operator is invalid for atomic vectors

aMat$x1
## Error: $ operator is invalid for atomic vectors
aMat$x
## Error: $ operator is invalid for atomic vectors
aMat$y
## Error: $ operator is invalid for atomic vectors

aArray$x1
## Error: $ operator is invalid for atomic vectors
aArray$x
## Error: $ operator is invalid for atomic vectors
aArray$y
## Error: $ operator is invalid for atomic vectors

aList$x1
## [1] 1
aList$x
## NULL
aList$y
## [1] 3

Extrat multiple element i <- c(1, 2), use x[i]

i <- c(1, 2)
aVec[i]
## x1 x2 
##  1  2
aMat[i]
## [1] 1 2
aArray[i]
## [1] 1 2
aList[i]
## $x1
## [1] 1
## 
## $x2
## [1] 2
bList[i]
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 2

Extrat multiple element i <- c(1, 2), use x[[i]]

i <- c(1, 2)
aVec[[i]]  # wrong
## Error: attempt to select more than one element
aMat[[i]]  # wrong
## Error: attempt to select more than one element
aArray[[i]]  # wrong
## Error: attempt to select more than one element
aList[[i]]  # wrong
## Error: subscript out of bounds
bList[[i]]  # wrong
## Error: subscript out of bounds

A Chinese blog is available here.