在R中找到两个骰子的最小值

Find minimum value of two dice roll in R

而不是使用循环在 R 中找到最少两次掷骰子,例如:

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]) )
  }
}

是否有更简单的方法或更短的代码来获得以下结果?

     [,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 循环就像 for 循环一样,但它们是很好的单行代码。

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
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

@user2974951 的答案要好得多,但无论如何这是我的解决方案:

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