Find minimum value of two dice roll in R - 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

Related

How to select unique columns in an R matrix

I want to select the unique columns in the matrix six on a simulation as follows:
> set.seed(3)
> sam = replicate(100, sample(1:3, 4, rep = T))
> (six = sam[,colSums(sam)==6])
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2 1 1 1 1 1 1 1 2 1
[2,] 2 2 3 1 1 2 2 1 2 2
[3,] 1 1 1 1 2 1 1 3 1 1
[4,] 1 2 1 3 2 2 2 1 1 2
I would like to end up with a matrix as:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 2 1 1 1 1 1
[2,] 2 2 3 1 1 1
[3,] 1 1 1 1 2 3
[4,] 1 2 1 3 2 1
Use unique function with MARGIN=2 and it will return a matrix with duplicated columns removed, by default, unique removes duplicated rows:
unique(six, MARGIN = 2)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 2 1 1 1 1 1
#[2,] 2 2 3 1 1 1
#[3,] 1 1 1 1 2 3
#[4,] 1 2 1 3 2 1
We can use duplicated
six[,!duplicated(t(six))]
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 2 1 1 1 1 1
#[2,] 2 2 3 1 1 1
#[3,] 1 1 1 1 2 3
#[4,] 1 2 1 3 2 1

How to find all the possible permutations of a matrix in R?

I have a matrix, for example, 5x5.
[,1] [,2] [,3] [,4] [,5]
[1,] 22 -2 -2 -2 2
[2,] -2 22 2 2 2
[3,] -2 2 22 2 2
[4,] -2 2 2 22 2
[5,] 2 2 2 2 22.
As you can see, the matrix is symmetric.
Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices.
But some of them are equivalent.
So,my problem is how to find the non-equivalent matrices from the 120.
I am taking about permutation matrices, because if I pick one of the 120 matrices and I use rmperm, I have as a result one (random) of the 120 matrices.
When I have 5x5 and 6x6 matrices, I don't have problem, because I have developed an algorithm. But now I want to do the same in a 7x7 matrix and more, but the algorithm is very slow, because I have lots of loops.
So, I want with one command, when I pick a matrix from the 120 matrices, to give me ALL the permutations matrices from the 120.
Thanks a lot!
Basically, you're asking for all row/column permutations. For an n x n matrix there are n! (n factorial) permutations of the rows and n! permutations of the columns, for a total of (n!)^2 total row/column permutations (not all of which are necessarily unique).
The first step would be to obtain a sample dataset and get the set of all permutations of the row/column indices (I'm assuming square matrices but it would be easy to extend to the non-square case):
# Sample dataset:
library(sna)
set.seed(100)
(g <- rgraph(3))
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 1 0 0
# [3,] 1 1 0
# All permutations of indices
library(gtools)
(perms <- permutations(nrow(g), nrow(g)))
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 3 2
# [3,] 2 1 3
# [4,] 2 3 1
# [5,] 3 1 2
# [6,] 3 2 1
You can compute all pairings of the row/column orderings, which you can use to grab all possible row/column permutations:
pairings <- expand.grid(1:nrow(perms), 1:nrow(perms))
head(pairings)
# Var1 Var2
# 1 1 1
# 2 2 1
# 3 3 1
# 4 4 1
# 5 5 1
# 6 6 1
all.perms <- lapply(1:nrow(pairings), function(x) g[perms[pairings[x,1],], perms[pairings[x,2],]])
head(all.perms)
# [[1]]
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 1 0 0
# [3,] 1 1 0
#
# [[2]]
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 1 1 0
# [3,] 1 0 0
# ...
Finally, you can use unique to grab the elements of all.perms that are unique matrices:
all.unique.perms <- unique(perms)
length(all.unique.perms)
# [1] 18
Basically what you want is permutation of multiset. The package iterpc will do the job.
> library(iterpc)
> I <- iterpc(c(3,7), ordered=TRUE)
> getlength(I)
[1] 120
> getall(I)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 2 2 2 2 2 2 2
[2,] 1 1 2 1 2 2 2 2 2 2
[3,] 1 1 2 2 1 2 2 2 2 2
[4,] 1 1 2 2 2 1 2 2 2 2
[5,] 1 1 2 2 2 2 1 2 2 2
[6,] 1 1 2 2 2 2 2 1 2 2
[7,] 1 1 2 2 2 2 2 2 1 2
[8,] 1 1 2 2 2 2 2 2 2 1
[9,] 1 2 1 1 2 2 2 2 2 2
[10,] 1 2 1 2 1 2 2 2 2 2
[11,] 1 2 1 2 2 1 2 2 2 2
[12,] 1 2 1 2 2 2 1 2 2 2
[13,] 1 2 1 2 2 2 2 1 2 2
[14,] 1 2 1 2 2 2 2 2 1 2
[15,] 1 2 1 2 2 2 2 2 2 1
[16,] 1 2 2 1 1 2 2 2 2 2
[17,] 1 2 2 1 2 1 2 2 2 2
[18,] 1 2 2 1 2 2 1 2 2 2
[19,] 1 2 2 1 2 2 2 1 2 2
[20,] 1 2 2 1 2 2 2 2 1 2
[ reached getOption("max.print") -- omitted 100 rows ]
Each row here is a permutations of 1 and 2. You should replace the 1's by -2.

Apply family of functions in R

I have a double loop in this form:
for (j in 1:col(mat))
{
for (i in 1:nrow(mat))
{
decision[i][j] = ifelse(mat[i][j] > 3, 1, 0)
}
}
Is there any way this functionality can be achieved through one of the apply functions with significantly improved speed?
You don't need any loops. If you create a matrix that looks like this
mat<-matrix(sample(1:10, 5*7, replace=T), ncol=7)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,] 9 6 10 7 6 10 6
#[2,] 7 6 3 3 6 8 3
#[3,] 7 9 7 5 6 7 6
#[4,] 2 3 8 6 10 1 5
#[5,] 4 1 5 6 1 10 6
then you can just do
decision <- (mat>3)+0
decision;
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#[1,] 1 1 1 1 1 1 1
#[2,] 1 1 0 0 1 1 0
#[3,] 1 1 1 1 1 1 1
#[4,] 0 0 1 1 1 0 1
#[5,] 1 0 1 1 0 1 1
R is a vectorized language, so for this kind of simple manipulation of a matrix apply type functions are not needed, just do:
decision <- ifelse(mat > 3, 1, 0)
(I'm assuming you want to iterate through the elements of the matrix, which means you would say ncol(mat) in your loop; col gives something rather different).

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

how to bind the same vector multiple times?

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

Resources