Matrix is a higher dimension arrangement of vectors. We can easily change a vector into matrix by assigning a dim attribute to the vector:
dim(vec) <- c(2,3)
Also we can access the dimension of a matrix by dim() too.
The dim() servers as a setter and a getter at the same time.
Note that the order of numbers is to fill up first column first.
The formal way to create a matrix is by matrix() as:
m<-matrix(1:6,2,3)
Also we can give a name for the rows or columns of a matrix by colnames and rownames. To access the names, we could use the same functions.
To retrieve the values with in a matrix, we could use the same way as for a vector. The order of index is row-id first. This is consistent with the way we populate a matrix. We changing row-id first while keep other indices fixed.
So to retrieve the value at (1,2) is:
m[1,2]
Data.frame is the list-version of matrix. Because matrix requires every element in it has the same type. Without data.frame, it is inconvenient to pack all data from the same resource into one structure.
data.frame is used to create a new data frame.
The operation on data frame is similar to how we operate matrix. The names returns the colnames, because typically each row contains an entry of data and different columns are different variables.