March 20, 2022

R Matrices

Matrices in R

Two dimensional R data structure, with all elements in rows and columns having same datatype.

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
num.matrix <- matrix(1:12, 4) # numeric matrix
matrix(1:6, nrow = 3, ncol=2)
Perf_Matrix <- matrix(1:6, nrow=2)
matrix(1.01:9.99, byrow = TRUE, nrow = 3) # floating point matrix
matrix(letters[1:9], byrow = FALSE, nrow = 3) # character matrix
matrix(1:6, nrow=2)<6 # logical matrix
log_mat <- matrix(rep(c(TRUE, FALSE), 3), nrow=2)
column<-matrix(rep(1:100,100), nrow=100, byrow=T)
matrix(1:6, 2, , FALSE)
dice_matrix <- matrix(dice_vector, nrow = 3, byrow = T)
M <- matrix(seq(1,16), 4, 4)
> x <- matrix(rnorm(9), nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
mdat <- matrix(c(1, 2, 3, 11, 12, 13), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3")))
matrix(nrow=3, ncol=3) # NA matrix
matrix(scan('matrix9.dat'), nrow=3, byrow=TRUE)

cbind(…, deparse.level = 1) # column binding
> cbind(c(1,2,3), c(4,5,6))
cbind(2:4, c(9,3,5))
cbind(0, rbind(1, 1:3))
cbind(I = 0, X = rbind(a = 1, b = 1:3))
all_wars_matrix <- cbind(star_wars_matrix,worldwide_vector)

rbind(…, deparse.level = 1, make.row.names = TRUE, stringsAsFactors = default.stringsAsFactors()) # row binding
> rbind(c(1,2,3), c(4,5,6))
mat <- rbind(1:3, c(6,9,2))
rbind(1:4, c = 2, "a++" = 10, dd, deparse.level = 2)

> attributes(a)
> class(a)
[1] "matrix"
mode(matrix9)
length(mat3)
length(mat[2, ])
dim(dice_matrix)
dim(mat)[3]
dimnames(x)[[1]] <- letters[1:8]
dimnames(y)[[2]] < paste("observation", 1:4, sep="")

> colnames(x)
> colnames(x) <- c("C1", "C2", "C3")
colnames(last_5) <- paste0("year_", 1:5)
> rownames(x)
> rownames(x) <- c("R1", "R2", "R3")
rowSums(x, na.rm = FALSE, dims = 1)
rowMeans(x, na.rm = FALSE, dims = 1)
colSums(x, na.rm = FALSE, dims = 1)
colMeans(x, na.rm = FALSE, dims = 1)
rowsum(x, group, reorder = TRUE, na.rm = FALSE, ...)

> A[1:4]
> x[ , ]    # leaving row, column field blank will select entire matrix
> x[c(1,2), c(2,3)]    # select rows 1 & 2 and columns 2 & 3
> x[c(3,2), ]    # leaving column field blank will select all columns
> mat[c(3, 5, 7)]
my_matrix[1:3, 2:4]
x[cbind(c(1,3,2), c(3,3,2))] 
x[c(1,3,2), c(3,3,2)]
mat[8] # 8th element in matrix, count column wise
## We can use negative integers to specify rows or columns to be excluded.
> x[-1, ]    # select all rows except first
> x[1,,drop=FALSE]  # result is a 1X3 matrix rather than a vector
> x[c(TRUE,FALSE,TRUE), c(TRUE,TRUE,FALSE)]
> x[, "A"]
> x[TRUE, c("A","C")]

> x[x%%2 == 0]    # select even elements
> x[x>4]    # select elements greater than 5
mat > 10

> x[2, 2] <- 10    # modify a single element
> x[x<5] <- 0    # modify elements less than 5
> rbind(x, c(1, 2, 3))    # add row
> cbind(x, c(1, 2, 3))    # add column
> x <- x[1:2, ]    # remove last row
> dim(x) <- c(3, 2)    # change to 3X2 matrix
write.matrix(x, file = "", sep = " ", blocksize)
sum(mat)
sum(3*col_mat^3/(1+row_mat*col_mat))

mat1 %*% mat2 # matrix multiplication
crossprod(x, y = NULL) # matrix cross-product
z <- crossprod(1:4)
tcrossprod(x, y = NULL)
tcrossprod(as.matrix(x))
> t(x)    # transpose a matrix

as.matrix # attempts to turn its argument into a matrix.
as.matrix(warpbreaks[1:10, ])
x  <- as.matrix(mtcars)
is.matrix # tests if its argument is a (strict) matrix.
is.matrix(as.matrix(1:10))

Related Articles:   Data Table in R        R lists


No comments:

Post a Comment