Indexing

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

How indexing is formatted:

variable[ row(s), column(s) ]

# Create a variables of five elements
x <- c(1, 4, 9, 16, 25)
y <- c(1, 4, 4, 36, 24)
m <- data.frame(x, y)
# Select the first, third, and fifth elements
x[c(1,3,5)]
[1]  1  9 25
# Select of all EXCEPT the second and fourth elements
x[c(-2, -4)]
[1]  1  9 25
# Select of all EXCEPT the second and fourth elements
x[c(TRUE, FALSE, TRUE, FALSE, TRUE)]
[1]  1  9 25
# Select the entire first row of a matrix, array, or dataframe
m[1, ]
  x y
1 1 1
# Select an the entire first column of a matrix, array, or dataframe
m[, 1]
[1]  1  4  9 16 25