我想使用向量c(1,3,5,12,24,60,120)自动生成以下三角形表来确定边长/高度
[1] [2] [3] [4] [5] [6] [...] [120]
[1.1] 1
[3.1] 1
[3.2] 1 1
[3.3] 1 1 1
[5.1] 1
[5.2] 1 1
[5.3] 1 1 1
[5.4] 1 1 1 1
[5.5] 1 1 1 1 1
[....]
[120.120] 1 1 1 1 1 1 1
答案 0 :(得分:1)
vec <- c(1,3,5,12,24,60,120)
library(plyr)
## make triangle matrices of each size
matrices <- sapply(vec, FUN = function(x) {
as.data.frame(lower.tri(matrix(rep(NA,x^2), ncol = x), diag = TRUE))
})
out <- 1 * rbind.fill(matrices)
out[is.na(out)] <- 0
注意:刚刚为c(1,3,5)做了提供示例
## > out
## V1 V2 V3 V4 V5
## 1 1 0 0 0 0
## 2 1 0 0 0 0
## 3 1 1 0 0 0
## 4 1 1 1 0 0
## 5 1 0 0 0 0
## 6 1 1 0 0 0
## 7 1 1 1 0 0
## 8 1 1 1 1 0
## 9 1 1 1 1 1