Matrices vs. Data Frames
Matrices and data frames are two ways to structure 2-dimensional information. They are different in a few ways. Generally, use data frames if the variable types are not all numeric.
> matrix.1<- matrix(1:16,4,4)
>
> matrix.1
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
>
> str(matrix.1) #str() asks for the structure of the object
int [1:4, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
>
> is.matrix(matrix.1) # The command form is.type(object) is basically asking if the ojbect matrix.1 is a matrix.
[1] TRUE
>
> is.data.frame(matrix.1)
[1] FALSE
> data.1 <- as.data.frame(matrix.1)
> data.1
V1 V2 V3 V4
1 1 5 9 13
2 2 6 10 14
3 3 7 11 15
4 4 8 12 16
>
>
> str(data.1)
'data.frame': 4 obs. of 4 variables:
$ V1: int 1 2 3 4
$ V2: int 5 6 7 8
$ V3: int 9 10 11 12
$ V4: int 13 14 15 16
>
>
> object.size(matrix.1)
264 bytes
> object.size(data.1)
1048 bytes
>
The object.size commands indicate how much memory the two types of data structure take up in the computer; note that the matrix takes up much less memory than the data frame.