Beginner R-data matrix and R representation: c(), length(), mode(), ":", matrix(), t(), "+", "-", "%* %", diag( ), solve(), eigen(), svd(), etc.

One, create a vector

1. In R, you can use the function c() to create a vector

x1=c(171,175,159,155,152,160)
y1=c(57,64,41,38,35,40)

Insert picture description here

2. In R, you can use the function length() to view the vector length

length(x1)

Insert picture description here

3. In R, you can use the function mode() to view the data type

mode(x1)

Insert picture description here

4. R language uses ":" to quickly generate arithmetic sequence

a = 1:12
a

Insert picture description here

b = c(1,3,6:4,9)
b

Insert picture description here

Two, create a matrix

(1) The x1 and y1 are all row vectors, which can also be regarded as a matrix with 1 row and 12 columns

1. R language uses matrix() to create a matrix

matrix(data = NA,nrow = 1,ncol = 1,byrow = FALSE,dimnames = NULL) #default value of each parameter

The data item is a necessary matrix element.
nrow is the number of rows.
ncol is the number of columns
. The product of nrow and ncol should be the number of matrix elements. The
byrow item controls whether to arrange the elements in rows or not
dimnames give the names of rows and columns.

A = matrix(c(1,4,2,5,3,6),nrow=2,ncol=3);
print(A)

Insert picture description here

B = matrix(c(1,2,3,4,5,6),3,2)
print(B)

Insert picture description here

C = matrix(x1,2,3)
print(C)

Insert picture description here

D = matrix(x1,3,2)
print(D)

Insert picture description here

E = matrix(x1,3,2,byrow = T) #创建按行排列的矩阵
print(E)

Insert picture description here

Three, matrix transpose

1. R language uses t() or transpose() to transpose the matrix

print(A)
print(t(A))

Insert picture description here

Four, matrix addition and subtraction

R can only add and subtract matrices with the same column in the same row

1. The R language uses "+" to add operations to the same column of the matrix

print(A[,1:2])
print(B[1:2,])
print(A[,1:2] + B[1:2,])

Insert picture description here

2. R language uses "-" to subtract matrices in the same column

print(A[,1:2])
print(B[1:2,])
print(A[,1:2] - B[1:2,])

Insert picture description here

Five, matrix multiplication

When A is m * n matrix and B is n * m matrix, the product of A and B can be obtained

1. R language uses "%* %" to multiply matrices

print(A)
print(B)
print(A %*% B)

Insert picture description here

print(A)
print(B)
print(B %*% A)

Insert picture description here

Six, the diagonal operation of the matrix

1. The R language uses the diag() function to take the diagonal elements of a square matrix

F = matrix(1:9,3,3)
print(F)
print(diag(F))

Insert picture description here

2. The R language uses the diag() function to generate a diagonal matrix with the vector in the brackets as diagonal elements. For a positive integer k, a k-dimensional unit vector will be generated

print(diag(diag(F)))

Insert picture description here

print(diag(4))

Insert picture description here

Seven, matrix inversion

1. R language uses the solve() function to perform matrix inversion

G = matrix(c(14,32,32,77),2,2)
print(G)

print(solve(G))

Insert picture description here
R language uses solve(A,b) to solve the linear equations Ax = b. If b is default, the system defaults to the identity matrix, so it can be used for matrix inversion.

Eight, the eigenvalues ​​and vectors of the matrix

1. R language uses eigen() function to find eigenvalues ​​and eigenvectors

eigen(x,symmetric,only.values = FALSE,EISPACK = FALSE)

Among them: x is a matrix, symmetric indicates whether the matrix x is a symmetric matrix, if not specified, the system will automatically monitor whether x is a symmetric matrix

x1=c(17,22,27,22,29,36,27,36,45)
D = matrix(x1,3,3)
print(D)

D.e = eigen(D,symmetric = T)
print(D.e)

Insert picture description here

Nine, matrix singular value decomposition

1. In R, you can use the svd() function for singular value decomposition

A = matrix(1:18,3,6)
print(A)

Insert picture description here

A.s = svd(A)
print(A.s)

Insert picture description here

Ten, the dimension of the matrix

1. Use the dim() function in R to return the dimension of a matrix

A = matrix(1:18,3,6)
print(A)

dim(A)

Insert picture description here

2. Use the nrow() function in R to return the number of rows of a matrix

A = matrix(1:18,3,6)
print(A)

nrow(A)

Insert picture description here

3. Use the ncol() function in R to return the number of columns of a matrix

A = matrix(1:18,3,6)
print(A)

ncol(A)

Insert picture description here

11. Sum of Matrix

1. Use the sum() function in R to sum the elements of the entire matrix

A = matrix(1:18,3,6)
print(A)

sum(A)

Insert picture description here

2. Use the rowSums() function in R to sum the elements of the matrix row by row

A = matrix(1:18,3,6)
print(A)

rowSums(A)

Insert picture description here

3. Use the colSums() function in R to sum the elements of the matrix row by row

A = matrix(1:18,3,6)
print(A)

colSums(A)

Insert picture description here

12. The mean value of the matrix

1. Use the mean() function in R to average the entire matrix,

A = matrix(1:18,3,6)
print(A)

mean(A)

Insert picture description here

2. Use the rowMeans() function in R to average the matrix by row

A = matrix(1:18,3,6)
print(A)

rowMeans(A)

Insert picture description here

3. Use the colMeans() function in R to average the matrix by column

A = matrix(1:18,3,6)
print(A)

colMeans(A)

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45154565/article/details/109235269