R Tips

Home

Contact

Steven Holland

Matrices and data frames

25 August 2009

Data frames and matrices are similar objects in R, but differ in that all elements of a matrix must be of the same type (integer, numeric, character, boolean, etc.), whereas a data frame can contain values of different types. Matrices and data frames can often be used interchangeably, but you may run into an operation that requires one or the other. One case occurs when you try to use the $ operator to select the column of a matrix:

> alphabeta2 <- matrix1$alpha + matrix1$beta
Warning messages:
1: $ operator is deprecated for atomic vectors, returning NULL in: matrix1$alpha
2: $ operator is deprecated for atomic vectors, returning NULL in: matrix1$beta

The $ operator can be used to select the column only for data frames. Since data frames are so similar to matrices, you can force a matrix to be treated as a data frame with the command as.data.frame(). Once you convert the matrix to a data frame, you can use the $ notation.

matrix1 <- as.data.frame(matrix1)
> alphabeta2 <- matrix1$alpha + matrix1$beta

Note that by assigning the result in the first line to matrix1, you are overwriting the data frame matrix1 with the data frame called matrix 1. This will not often be a problem for you, but if you ever encounter a warning message like the ones above, try coercing your object to a data frame.