Section 3 Matrix

3.1 Function

Example vector: x <- c(1:12)

Function Explanation Example
matrix Create a matrix matrix(data=x, nrow=4, ncol=3, byrow=TRUE)
as.matrix Convert a collection of vectors into matrix as.matrix(x)
is.matrix Check if its argument is a (strict) matrix is.matrix(x)
dim Get the dimension of a matrix dim(x)
dimnames A dimnames attribute for the matrix, NULL or a list of length 2 dimnames(x)
rownames The row names of the matrix rownames(x)
colnames The column names of the matrix colnames(x)
cbind, rbind Column or row bind vectors to create a matrix cbind(x,x)
attributes Attributes of a matrix attributes(x)
data.matrix Attempts to convert a dataframe to a numeric matrix data.matrix(x)

3.2 Example

?matrix

x <- c(1:12)
x
is.matrix(x)

y <- as.matrix(x)
y
is.matrix(y)
dim(y)
dimnames(y)

u <- matrix(data=x, nrow=4, ncol=3)
u

v <- x
dim(v) <- c(4, 3)
v

z <- matrix(data=x, nrow=4, ncol=3, byrow=TRUE)
z
is.matrix(z)
dim(z)
dimnames(z)

dimnames(z) <- list(LETTERS[1:4], letters[1:3])
dimnames(z)
attributes(z)

str(z)

rownames(z)
colnames(z)

rbind(x,x)
cbind(x,x)