how to bind the same vector multiple times? - r

How can I bind the same vector o = c(1,2,3,4) multiple times to get a matrix like:
o = array(c(1,2,3,4,1,2,3,4,1,2,3,4), dim(c(4,3))
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
[4,] 4 4 4
In a nicer way than: o = cbind(o,o,o) and maybe more generalized (duplicate)? I need this to specify colors for elements in textplot.

R recycles. It's very eco-friendly:
o=c(1,2,3,4)
> matrix(o,nrow = 4,ncol = 4)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4

You can use replicate
> o = c(1,2,3,4)
> replicate(4, o)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4

You can use outer
outer(1:4,1:4,function(x,y)x)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4

Related

Find minimum value of two dice roll in R

Instead of using a loop to find a minimum of two dice roll in R such as:
Min <- matrix(0, nrow=6, ncol=6)
d1 <- 1:6
d2 <- 1:6
for(k in 1:6){
for(j in 1:6){
Min[k,j] <- min( c(d1[k], d2[j]) )
}
}
Is there a simpler way or shorter code to get the following result?
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 1 2 2 2 2 2
[3,] 1 2 3 3 3 3
[4,] 1 2 3 4 4 4
[5,] 1 2 3 4 5 5
[6,] 1 2 3 4 5 6
outer(d1,d2,pmin)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 1 2 2 2 2 2
[3,] 1 2 3 3 3 3
[4,] 1 2 3 4 4 4
[5,] 1 2 3 4 5 5
[6,] 1 2 3 4 5 6
*apply loops are loops just like for loops but they make nice one-liners.
sapply(1:6, function(y) sapply(1:6, function(x) min(x, y)))
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 1 1 1 1 1
#[2,] 1 2 2 2 2 2
#[3,] 1 2 3 3 3 3
#[4,] 1 2 3 4 4 4
#[5,] 1 2 3 4 5 5
#[6,] 1 2 3 4 5 6
#user2974951's answer is way better, but here's my solution anyways:
a <- matrix(0, nrow = 6, ncol = 6)
for (i in 6:1){
a[,i] <- i
a[i,] <- i
}
a
#> [,1] [,2] [,3] [,4] [,5] [,6]
#>[1,] 1 1 1 1 1 1
#>[2,] 1 2 2 2 2 2
#>[3,] 1 2 3 3 3 3
#>[4,] 1 2 3 4 4 4
#>[5,] 1 2 3 4 5 5
#>[6,] 1 2 3 4 5 6

Rank numbers by comparing two matrices

Having a matrix A like:
[,1] [,2] [,3] [,4]
[1,] 1 4 7 6
[2,] 2 5 8 1
[3,] 5 1 7 8
and a matrix B like:
[,1]
[1,] 8
[2,] 6
[3,] 1
[4,] 7
[5,] 5
[6,] 2
[7,] 3
[8,] 4
I want to get create a matrix C similar to A replacing A values with the rank of A values in matrix B. The result should be:
matrix C
[,1] [,2] [,3] [,4]
[1,] 3 8 4 2
[2,] 6 5 1 3
[3,] 5 3 4 1
You can use match and adjust the dimensions:
C <- match(A, B)
dim(C) <- dim(A)
--
Example:
> set.seed(123)
> (A <- matrix(sample(1:8), ncol = 4))
[,1] [,2] [,3] [,4]
[1,] 3 8 4 2
[2,] 6 5 1 7
> (B <- matrix(sample(1:8), ncol= 1))
[,1]
[1,] 5
[2,] 4
[3,] 6
[4,] 3
[5,] 8
[6,] 2
[7,] 1
[8,] 7
> (C <- match(A, B))
[1] 4 3 5 1 2 7 6 8
> (dim(C) <- dim(A))
[1] 2 4
> C
[,1] [,2] [,3] [,4]
[1,] 4 5 2 6
[2,] 3 1 7 8

Matrix indices ordered by the value they contain

I have a matrix like this:
mat<-matrix(c(10,45,2,15,3,98,1,7,13),nrow = 3)
mat
[,1] [,2] [,3]
[1,] 10 15 1
[2,] 45 3 7
[3,] 2 98 13
I want to get the indices of ordered values, as what we can get from order(x, arr.idx = T) but applied to a matrix. That is:
[,1] [,2]
1 3
3 1
2 2
2 3
1 1
3 3
1 2
2 1
3 2
Is it there a fast way to do it?
Thank you in advance
You can use
arrayInd(order(mat), dim(mat), dimnames(mat))
# [,1] [,2]
# [1,] 1 3
# [2,] 3 1
# [3,] 2 2
# [4,] 2 3
# [5,] 1 1
# [6,] 3 3
# [7,] 1 2
# [8,] 2 1
# [9,] 3 2
Using the order as index, we rearrange the row and col of 'mat' and cbind it to get the row/column index of the ordered values
i1 <- order(mat)
cbind(row(mat)[i1], col(mat)[i1])
# [,1] [,2]
#[1,] 1 3
#[2,] 3 1
#[3,] 2 2
#[4,] 2 3
#[5,] 1 1
#[6,] 3 3
#[7,] 1 2
#[8,] 2 1
#[9,] 3 2

Partitioned matrices in R

I’m looking for a code to create matrix B from matrix A, this is a very simple example my real matrix A is (500 x500) and B11(50x50)
1 2 = A
3 4
1 1 | 2 2
1 1 | 2 2
.----------= B
3 3 | 4 4
3 3 | 4 4
Thanks in advance.
You want a Kronecker product, which is %x%:
R>A <- matrix(1:4,2,2)
R>A
[,1] [,2]
[1,] 1 3
[2,] 2 4
R>X <- matrix(1,2,2)
R>X
[,1] [,2]
[1,] 1 1
[2,] 1 1
R>A %x% X
[,1] [,2] [,3] [,4]
[1,] 1 1 3 3
[2,] 1 1 3 3
[3,] 2 2 4 4
[4,] 2 2 4 4
R>t(A) %x% X
[,1] [,2] [,3] [,4]
[1,] 1 1 2 2
[2,] 1 1 2 2
[3,] 3 3 4 4
[4,] 3 3 4 4

R matlab package: why is repmat inconsistent?

I have got a question regarding the matlab package for R. Here's what I get
library(matlab)
a = matrix(1:4,2,2)
repmat(a,3,1)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 1 2
[4,] 3 4
[5,] 1 2
[6,] 3 4
this is what I expect. replicate a three times along the first dimension. but
b = matrix(1:6,2,3)
b
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
repmat(b,3,1)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 1 2
[5,] 3 4
[6,] 5 6
[7,] 1 2
[8,] 3 4
[9,] 5 6
this is not consistent. I want a 6 by 3 matrix as the one obtained by
rbind(b,rbind(b,b))
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 1 3 5
[4,] 2 4 6
[5,] 1 3 5
[6,] 2 4 6
It just appears to be transposing the matrix before doing the stacking. You could just transpose your matrix before sending it into repmat
> repmat(t(b), 3, 1)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 1 3 5
[4,] 2 4 6
[5,] 1 3 5
[6,] 2 4 6

Resources