help(R): matrix indexing

19 Jan

In R, some want to treat matrix like a data frame. It is a bad idea. And no – one cannot use the dollar sign to pick out a column of a matrix. The underlying data structure for a matrix in R is a vector, whereas data.frame object in R is of type list (see using typeof(object)) and it carries within it vectors, reachable using the dollar sign followed by name of the vector (a way to only access elements within lists).


> a <- data.frame(ab=1:2, bb=1:2)
> b <- matrix(ncol=2, nrow=2)
> colnames(b) <- c("ab", "bb")

> a$ab
[1] 1 2

> b$bb
Error in b$bb : $ operator is invalid for atomic vectors

# here's why
> dput(a)
structure(list(ab = 1:2, bb = 1:2), .Names = c("ab", "bb"), row.names = c(NA, 
-2L), class = "data.frame")

> dput(b)
structure(c(NA, NA, NA, NA), .Dim = c(2L, 2L), .Dimnames = list(NULL, c("ab", "bb")))

# list 
> c <- list(ab=c(1,2,3), bb=c(1,2,3))

> dput(c)
structure(list(ab = c(1, 2, 3), bb = c(1, 2, 3)), .Names = c("ab", 
"bb"))

# so you can do 
c$ab
[1] 1 2 3

# for matrices, data frames, etc. one can however use 
a[,"ab"]
b[,"ab"]

# or
b[,2]

# for people wishing to do so, here's why treating matrices as data frames is a bad idea -
> a <- matrix(ncol=2, nrow=2)
> a[,1] <- 1:2
> a[,2] <- c("abc", "bbc")
> str(a[,1])
chr [1:2] "1" "2"
> a[,1] <- as.numeric(a[,1])
> str(a[,1])
 chr [1:2] "1" "2"

# Notes on coercion
a <- matrix(ncol=2, nrow=3)
> a$b <- 1:2
Warning message:
In a$b <- 1:2 : Coercing LHS to a list
> typeof(a)
[1] "list"