copy rownames to new column in a matrix in r

You can copy the rownames of a matrix to a new column by creating a new column and assigning the rownames to it. Here's how you can do it:

main.r
# Create a matrix
mat <- matrix(1:9, nrow = 3)
rownames(mat) <- c("row1", "row2", "row3")

# Copy rownames to a new column
mat <- cbind(row.names = rownames(mat), mat)

print(mat)
180 chars
9 lines

In the code above, we first create a matrix mat with rownames "row1", "row2", and "row3". Then, we use cbind() function to add a new column named "row.names" to the matrix with the rownames.

This will copy the rownames to the new column in the matrix.

related categories

gistlibby LogSnag