matrix()
c(17, 30, 30, 25, 23, 21)
age <-matrix(age, nrow=3, ncol=2, byrow=FALSE)
[,1] [,2]
[1,] 17 25
[2,] 30 23
[3,] 30 21
matrix(age, nrow=2, ncol=3, byrow=TRUE)) (ageMat <-
[,1] [,2] [,3]
[1,] 17 30 30
[2,] 25 23 21
c(19, 19, 31, 19, 24)
age <- c(95, 76, 94, 76, 76)
weight <- c(197, 178, 189, 184, 173)
height <-rbind(age, weight, height)
[,1] [,2] [,3] [,4] [,5]
age 19 19 31 19 24
weight 95 76 94 76 76
height 197 178 189 184 173
cbind(age, weight, height)
age weight height
[1,] 19 95 197
[2,] 19 76 178
[3,] 31 94 189
[4,] 19 76 184
[5,] 24 76 173
dim(ageMat)
[1] 2 3
nrow(ageMat)
[1] 2
ncol(ageMat)
[1] 3
prod(dim(ageMat))
[1] 6
t(ageMat)
[,1] [,2]
[1,] 17 25
[2,] 30 23
[3,] 30 21
as.matrix(1:3)
[,1]
[1,] 1
[2,] 2
[3,] 3
c(ageMat)
[1] 17 25 30 23 30 21
2
P <- 3
Q <- matrix(1:(P*Q), nrow=P, ncol=Q)) (pqMat <-
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
row(pqMat)) (rowMat <-
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
col(pqMat)) (colMat <-
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
cbind(rowIdx=c(rowMat), colIdx=c(colMat), val=c(pqMat))
rowIdx colIdx val
[1,] 1 1 1
[2,] 2 1 2
[3,] 1 2 3
[4,] 2 2 4
[5,] 1 3 5
[6,] 2 3 6
matrix(sample(1:10, 16, replace=TRUE), 4, 4)
mat <-upper.tri(mat)
[,1] [,2] [,3] [,4]
[1,] FALSE TRUE TRUE TRUE
[2,] FALSE FALSE TRUE TRUE
[3,] FALSE FALSE FALSE TRUE
[4,] FALSE FALSE FALSE FALSE
lower.tri(mat)
[,1] [,2] [,3] [,4]
[1,] FALSE FALSE FALSE FALSE
[2,] TRUE FALSE FALSE FALSE
[3,] TRUE TRUE FALSE FALSE
[4,] TRUE TRUE TRUE FALSE
Row and column indices
matrix(sample(16:35, 6, replace=TRUE), nrow=2, ncol=3)
ageMat <-2, 2] ageMat[
[1] 32
2, 2] <- 24
ageMat[2, 2] ageMat[
[1] 24
2, ] ageMat[
[1] 19 24 18
1] ageMat[ ,
[1] 26 19
ageMat[ , ]
[,1] [,2] [,3]
[1,] 26 28 26
[2,] 19 24 18
1, drop=FALSE] ageMat[ ,
[,1]
[1,] 26
[2,] 19
2:3] ageMat[ ,
[,1] [,2]
[1,] 28 26
[2,] 24 18
c(1, 3)] ageMat[ ,
[,1] [,2]
[1,] 26 26
[2,] 19 18
Index vector
c(1, 3, 4)
idxVec <- ageMat[idxVec]
[1] 26 28 24
Index matrix
ageMat
ageMatNew <- matrix(c(11, 21, 12, 22), nrow=2, ncol=2)) (replaceMat <-
[,1] [,2]
[1,] 11 12
[2,] 21 22
c(1, 3)] <- replaceMat
ageMatNew[ , ageMatNew
[,1] [,2] [,3]
[1,] 11 28 12
[2,] 21 24 22
Logical index matrix
ageMat >= 25) (idxMatLog <-
[,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] FALSE FALSE FALSE
ageMat[idxMatLog]
[1] 26 28 26
which(idxMatLog, arr.ind=TRUE)) (idxMatNum <-
row col
[1,] 1 1
[2,] 1 2
[3,] 1 3
ageMat[idxMatNum]
[1] 26 28 26
arrayInd(idxVec, dim(ageMat))) (idxMat <-
[,1] [,2]
[1,] 1 1
[2,] 1 2
[3,] 2 2
Also see help(Extract)
c(19, 19, 31, 19, 24)
age <- c(95, 76, 94, 76, 76)
weight <- c(197, 178, 189, 184, 173)
height <- cbind(age, weight, height)
mat <- order(mat[ , "age"])) (rowOrder1 <-
[1] 1 2 4 5 3
mat[rowOrder1, ]
age weight height
[1,] 19 95 197
[2,] 19 76 178
[3,] 19 76 184
[4,] 24 76 173
[5,] 31 94 189
order(mat[ , "age"], partial=mat[ , "weight"])
rowOrder2 <- mat[rowOrder2, ]
age weight height
[1,] 19 76 178
[2,] 19 76 184
[3,] 19 95 197
[4,] 24 76 173
[5,] 31 94 189
order(mat[ , "weight"], -mat[ , "height"])
rowOrder3 <- mat[rowOrder3, ]
age weight height
[1,] 19 76 184
[2,] 19 76 178
[3,] 24 76 173
[4,] 31 94 189
[5,] 19 95 197
array(1:12, c(2, 3, 2),
(myArr1 <-dimnames=list(row=c("f", "m"), column=c("CG", "WL", "T"),
layer=c("high", "low"))))
, , layer = high
column
row CG WL T
f 1 3 5
m 2 4 6
, , layer = low
column
row CG WL T
f 7 9 11
m 8 10 12
1, 3, 2] myArr1[
[1] 11
2, 1, 2] <- 19
myArr1[ myArr1*2
myArr2 <-"high"] myArr2[ , ,
column
row CG WL T
f 2 6 10
m 4 8 12
Switch rows and columns
aperm(myArr1, c(2, 1, 3))
, , layer = high
row
column f m
CG 1 2
WL 3 4
T 5 6
, , layer = low
row
column f m
CG 7 19
WL 9 10
T 11 12
Switch rows and layers
aperm(myArr1, c(3, 2, 1))
, , row = f
column
layer CG WL T
high 1 3 5
low 7 9 11
, , row = m
column
layer CG WL T
high 2 4 6
low 19 10 12
R markdown - markdown - R code - all posts