1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
fct <- function(M, V){
## Build an empty matrix with NaN values
## The number of rows and column is increased by one
res <- matrix(NaN, ncol=ncol(M)+1, nrow=nrow(M)+1,
dimnames = list(c(rownames(M),paste0("r",nrow(M)+1)),
c(colnames(M),paste0("c",ncol(M)+1))))
## Copy the matrix M in the first columns and rows of the new matrix res
res[1:nrow(M), 1:ncol(M)] <- M
## We assume that the order of names can be different between V and M
newPos <- sapply(rownames(V), function(v) which (v==rownames(M)))
## add the values in the last row and last col
res[1:nrow(M),nrow(M)+1] <- V[newPos,1]
res[nrow(M)+1, 1:nrow(M)] <- V[newPos,1]
return(res)
} |
Partager