数据类型
##矩阵
#创建矩阵
m <- matrix(1:8, nrow = 2)
#Q:按行创建矩阵
m <- matrix(1:8,nrow= 2, byrow = T)
m
matrix(c(1,5,2,6,3,7,4,8), nrow = 2) #byrow参数知识改变了数据的输入顺序,矩阵本身依然是按列存储
#矩阵运算
m1 <- matrix(1:4, nrow = 2)
m2 <- matrix(2:7, nrow = 2)
m1;m2
m1 + m2
m1 * m2
#矩阵运算,可以用循环补齐吗?
m1 <- matrix(1:4, nrow = 2)
m2 <- matrix(2:7, nrow = 2)
m1;m2
m1 + m2
m1 <- matrix(1:4, nrow = 2)
m2 <- matrix(2:3, nrow = 1)
m1;m2
m1 + m2
#矩阵索引
m <- matrix(1:12, nrow = 4)
m
m[,2:3]
m[1:3]
m[1:5]
#访问矩阵的某行
m
m[1,]
#访问矩阵的某列
m
m[,2]
m[,1<=2]
#矩阵元素按条件筛选
m <- matrix(1:12, nrow = 4)
m
first.col <- m[,1]
first.col
first.col %% 2 == 0
m[first