I have a question on how to fill in a matrix by its column names. I think the following example would make it clear what I want.
mat <- matrix(NA, nrow = 10, ncol = 5)
colnames(mat) <- c("Apple", "Orange", "Pear", "Grape", "Mango")
Now I have a new matrix whose row names are a subset of the column names in mat, and with an arbitrary order.
jmat <- matrix(rnorm(4), nrow = 4, ncol = 1)
rownames(jmat) <- sample(c("Apple", "Orange", "Grape", "Mango"))
I want to fill the first row of mat by the corresponding values in jmat. The value of “Apple” in jmat should be in the “Apple” column in mat, etc. Because there is no “Pear” row in jmat, so the “Pear” column in mat will still be a NA. What is the simplest way of doing that?
Thank you.
I found that
matchfunction can give the indices of a vector within another vector.