gistlib
# Create a sample matrix mat <- matrix(1:9, nrow = 3) rownames(mat) <- c("row1", "row2", "row3") colnames(mat) <- c("A", "B", "C") # Define the missing variable missing_var <- setdiff(rownames(mat), colnames(mat)) # Add the missing variable as a new column of zeros mat <- cbind(mat, matrix(0, nrow = nrow(mat), ncol = length(missing_var), dimnames = list(NULL, missing_var))) mat 383 chars12 lines
# Create a sample matrix mat <- matrix(1:9, nrow = 3) rownames(mat) <- c("row1", "row2", "row3") colnames(mat) <- c("A", "B", "C") # Define the missing variable missing_var <- setdiff(rownames(mat), colnames(mat)) # Add the missing variable as a new column of zeros mat <- cbind(mat, matrix(0, nrow = nrow(mat), ncol = length(missing_var), dimnames = list(NULL, missing_var))) mat
gistlibby LogSnag