a vector to an upper Triangle matrix by row in R
Here's one option
b[lower.tri(b, diag=FALSE)] <- a
b <- t(b)
b
# [,1] [,2] [,3] [,4]
# [1,] 0 1 2 3
# [2,] 0 0 4 5
# [3,] 0 0 0 6
# [4,] 0 0 0 0
Alternatively, reorder a
as required and assign that into the upper-right triangle:
ut <- upper.tri(b, diag=FALSE)
b[ut] <- a[order(row(ut)[ut], col(ut)[ut])]
b
[,1] [,2] [,3] [,4]
[1,] 0 1 2 3
[2,] 0 0 4 5
[3,] 0 0 0 6
[4,] 0 0 0 0