文章目录
vector
返回 index
使用 which
或者 match
, 如果有重复数据, match
返回第一个匹配的 index.
x <- sample(1:10,10,replace=TRUE)
> x
[1] 7 5 1 10 7 8 1 4 10 3
> which(c(7,8)==x)
[1] 1 5 6
> match(c(7,8),x)
[1] 1 6
>
list
追加
f <- list()
d <- c(1,2)
e <- c(3,4)
f <- list(d, e)
g <- c(7,8)
f <- c(list(g), f) # 追加到前面
h <- c(9, 0)
f <- c(f, list(h)) # 追加到后面
data.frame
追加
# 按列追加
f <- data.frame()
d <- c(1,2)
e <- c(3,4)
f <- data.frame(d, e)
g <- c(7,8)
f <- data.frame(g, f) # 追加到前面
h <- c(9, 0)
f <- data.frame(f, h) # 追加到后面
求最大值
max(f) # 如果全是 numeric 值, 可以用 max() 求最大值
matrix
防止一维 matrix 变成 vector
> a<-matrix(0,nrow=3,ncol=3,byrow=TRUE,
dimnames=list(c("row1","row2","row3"),
c("col1","col2","col3"))
> at[1,, drop=FALSE]
col1 col2 col3
row1 0 0 0
> is.matrix(at[1,, drop=FALSE])
[1] TRUE
reverse matrix 列
> a<-matrix(0,nrow=3,ncol=3,byrow=TRUE,
dimnames=list(c("row1","row2","row3"),
c("col1","col2","col3"))
> at[,ncol(at):1]
col3 col2 col1
row1 0 0 0
row2 0 0 0
row3 0 0 0
扩展的 matrix, Matrix Package
批量替换 Matrix 中的元素
给定 Matrix:
> library('Matrix')
> a <- Matrix(c(1,1,2, 1,1,2, 1,1,2, 1,1,2, 1,1,2), nrow = 5)
> a
5 x 3 Matrix of class "dgeMatrix"
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 1 1 2
[3,] 2 1 1
[4,] 1 2 1
[5,] 1 1 2
> is.matrix(a)
[1] FALSE
定义一个基本 matrix
> f <- matrix(c(1:2,1:2), ncol=2)
> f
[,1] [,2]
[1,] 1 1
[2,] 2 2
将基本 matrix 元素中的值为索引下标指示 Matrix 中的元素位置,并替换为所需要的值.
如本例则替换 Matrix 中
a
11
a_{11}
a11,
a
22
a_{22}
a22 中的值.
> a[f] = 3
> a
5 x 3 Matrix of class "dgeMatrix"
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 1 3 2
[3,] 2 1 1
[4,] 1 2 1
[5,] 1 1 2
[1]. https://cran.r-project.org/web/packages/Matrix/vignettes/Intro2Matrix.pdf
[2]. https://cran.r-project.org/web/packages/Matrix/
操作矩阵中的方阵
给定矩阵
m
×
n
m \times n
m×n, 其方阵部分维度为
⌊
m
×
n
⌋
=
min
{
m
,
n
}
\lfloor \sqrt{m\times n}\rfloor = \min \{ m, n \}
⌊m×n⌋=min{m,n}.
例如:
5
×
3
5\times 3
5×3,
⌊
5
×
3
⌋
=
⌊
3.872983
⌋
=
3
=
min
{
5
,
3
}
\lfloor \sqrt{5\times 3} \rfloor = \lfloor 3.872983 \rfloor = 3 = \min \{5, 3\}
⌊5×3⌋=⌊3.872983⌋=3=min{5,3}
> library('Matrix')
> a <- Matrix(c(1,1,2, 1,1,2, 1,1,2, 1,1,2, 1,1,2), nrow = 5)
> a
5 x 3 Matrix of class "dgeMatrix"
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 1 1 2
[3,] 2 1 1
[4,] 1 2 1
[5,] 1 1 2
> length(a)
[1] 15
> n <- sqrt(length(a))
> n
[1] 3.872983
> f<-matrix(c(1:n,1:n),ncol=2)
> f
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
> a[f] = 6
> a
5 x 3 Matrix of class "dgeMatrix"
[,1] [,2] [,3]
[1,] 6 2 1
[2,] 1 6 2
[3,] 2 1 6
[4,] 1 2 1
[5,] 1 1 2
删除指定列
Matrix::Matrix
与 base::matrix
操作方法相同
> a <- Matrix::Matrix(c(1,1,2, 1,1,2, 1,1,2, 1,1,2, 1,1,2), nrow = 5)
> a
5 x 3 Matrix of class "dgeMatrix"
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 1 1 2
[3,] 2 1 1
[4,] 1 2 1
[5,] 1 1 2
> a2 <- a[,-2] # delete the second column
> a2
5 x 2 Matrix of class "dgeMatrix"
[,1] [,2]
[1,] 1 1
[2,] 1 2
[3,] 2 1
[4,] 1 1
[5,] 1 2
> a13<- a[,-c(1,3),drop=FALSE] # delete the first and third column
> a13
5 x 1 Matrix of class "dgeMatrix"
[,1]
[1,] 2
[2,] 1
[3,] 1
[4,] 2
[5,] 1
删除指定行
使用 base::setdiff
得出要保留的行. Matrix::Matrix
与 base::matrix
操作方法相同.
> a03 <- a[base::setdiff(1:base::dim(a)[1], 3),] # remove the third row
> a03
4 x 3 Matrix of class "dgeMatrix"
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 1 1 2
[3,] 1 2 1
[4,] 1 1 2