ysw <- matrix(c(94,98,85,85,97,95,
96,98,96,96,98,95,
95,98,85,82,96,93),
ncol = 3,
dimnames = list(
c("张三","李四","王五","悟空","八戒","沙僧"),
c("yw","sx","wy")
)
) # 按列填充
ysw
ysw <- matrix(c(94,98,85,85,97,95,
96,98,96,96,98,95,
95,98,85,82,96,93),
ncol = 3) # 按列填充
colnames(ysw)<-c("yw","sx","wy")
row.names(ysw) <- c("张三","李四","王五","悟空","八戒","沙僧")
ysw
# yw sx wy
# 张三 94 96 95
# 李四 98 98 98
# 王五 85 96 85
# 悟空 85 96 82
# 八戒 97 98 96
# 沙僧 95 95 93
ysw <- matrix(c(94,96,95,
98,98,98,
85,96,85,
85,96,82,
97,98,96,
95,95,93),
byrow = TRUE,# 按行填充
ncol = 3)
colnames(ysw)<-c("yw","sx","wy")
row.names(ysw) <- c("张三","李四","王五","悟空","八戒","沙僧")
ysw
# yw sx wy
# 张三 94 96 95
# 李四 98 98 98
# 王五 85 96 85
# 悟空 85 96 82
# 八戒 97 98 96
# 沙僧 95 95 93
colnames(ysw)
# [1] "yw" "sx" "wy"
row.names(ysw)
# [1] "张三" "李四" "王五" "悟空" "八戒" "沙僧"
nrow(ysw) # [1] 6
ncol(ysw) # [1] 3
dim(ysw) # [1] 6 3
dimnames(ysw)
# [[1]]
# [1] "张三" "李四" "王五" "悟空" "八戒" "沙僧"
# [[2]]
# [1] "yw" "sx" "wy"
ysw[5,] # 通过下标
ysw['八戒',] # 通过名称
# yw sx wy
# 97 98 96
ysw[5,-3]
ysw[5,1:2] # 第一行后两个
# yw sx
# 97 98
ysw[,c("sx","yw","wy")]
# sx yw wy
# 张三 96 94 95
# 李四 98 98 98
# 王五 96 85 85
# 悟空 96 85 82
# 八戒 98 97 96
# 沙僧 95 95 93
order_sx <- order(ysw[,'sx'],decreasing = TRUE)
order_sx # [1] 2 5 1 3 4 6
ysw[order_sx,]
# yw sx wy
# 李四 98 98 98
# 八戒 97 98 96
# 张三 94 96 95
# 王五 85 96 85
# 悟空 85 96 82
# 沙僧 95 95 93
ysw2 <- matrix(c(88,89,89,96,95,96),
ncol = 3,
dimnames = list(
c("刘备","曹操"),
c("yw","sx","wy")
))
ysw2
# scff sx wy
# 刘备 88 89 95
# 曹操 89 96 96
rowAdd <- rbind(ysw,ysw2)
rowAdd
# yw sx wy
# 张三 94 96 95
# 李四 98 98 98
# 王五 85 96 85
# 悟空 85 96 82
# 八戒 97 98 96
# 沙僧 95 95 93
# 刘备 88 89 95
# 曹操 89 96 96
lszz <- matrix(c(88,98,85,85,97,95,
96,98,96,96,98,95),
ncol = 2,
dimnames = list(
c("张三","李四","王五","悟空","八戒","沙僧"),
c("ls","zz")
)
)
lszz
# ls zz
# 张三 88 96
# 李四 98 98
# 王五 85 96
# 悟空 85 96
# 八戒 97 98
# 沙僧 95 95
colAdd <- cbind(ysw,lszz)
colAdd
# yw sx wy ls zz
# 张三 94 96 95 88 96
# 李四 98 98 98 98 98
# 王五 85 96 85 85 96
# 悟空 85 96 82 85 96
# 八戒 97 98 96 97 98
# 沙僧 95 95 93 95 95
rowSums(ysw)
apply(ysw, 1,sum)
# 张三 李四 王五 悟空 八戒 沙僧
# 285 294 266 263 291 283
colMeans(ysw)
apply(ysw,2 ,mean)
# yw sx wy
# 92.33333 96.50000 91.50000
apply(ysw,2 ,function(x){
sd(x)/mean(x)
})
# yw sx wy
# 0.06339827 0.01269166 0.07074340
apply split->apply-combine: 先分组对每一组操作组合
A <- matrix(c(1,2,3,
2,2,5,
3,5,1),
ncol = 3,
byrow = TRUE)
b <- 1:3
solve(A,b) # [1] 1 0 0
An <- diag(3)
# cAn
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 0 1 0
# [3,] 0 0 1
solve(A,An)
# [,1] [,2] [,3]
# [1,] -1.5333333 0.86666667 0.26666667
# [2,] 0.8666667 -0.53333333 0.06666667
# [3,] 0.2666667 0.06666667 -0.13333333
solve(A) %*% A # 得到单位矩阵
# [,1] [,2] [,3]
# [1,] 1.000000e+00 8.881784e-16 1.054712e-15
# [2,] -4.440892e-16 1.000000e+00 -7.077672e-16
# [3,] -5.551115e-17 -1.110223e-16 1.000000e+00
sqrt(2)^2 == 2 # FALSE
dplyr::near(sqrt(2)^2,2) # TRUE
all(dplyr::near(solve(A) %*% A,diag(3))) # TRUE
矩阵本质就是二维数组
install.packages('imager', repos ="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
Ubuntu 需要安装
sudo apt-get install libfftw3-dev libtiff5-dev
library(imager)
img1 <- load.image("/home/wy/Pictures/7777777.jpg")
str(img1) # cimg [1:640, 1:427, 1, 1:3]
plot(img1)
img1[,,1]<-0 # 去掉红色图层
img1[,,3]<-0 # 去掉蓝色图层
plot(img1)
area_coor_x <- 350:449
area_coor_y <- 110:259
array_dim <- c(length(area_coor_x),length(area_coor_y),3)
array_dim # [1] 100 150 3
array_data <- runif(prod(array_dim))
array_data
random_noise <- array(dim = array_dim,data = array_data)
random_noise
img1[area_coor_x,area_coor_y,]<- (1-0.6) *
img1[area_coor_x,area_coor_y,]+
0.6*random_noise
plot(img1)
注意:硬盘上的任何数据都是R的六种数据(向量,因子,矩阵,数组,数据框,列表)之一