R generate triangle table based on a vector -


i automatically generate following triangle table using vector c(1,3,5,12,24,60,120) determine length/height of sides

           [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 

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 

note: done c(1,3,5) provide example

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

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -