Search code examples
rnames

Use values of a vector in row names in R?


I have a numeric vector vec <- c(1.5,1.3,5.7), a vector of Greek letters Greeks <- c("$\\alpha$","$\\beta$","$\\gamma$"), and a matrix M<-diag(3). I want to assign names to this table using latex symbols and the values of vec. Basically, I want to get:

vec <- c(1.5,1.3,5.7)
M<-diag(3)
rnames <- c("$\\alpha$ (1.5)","$\\beta$ (1.3)","$\\gamma$ (5.7)")
rownames(M) <- rnames

In my real case scenario, vec is of variable length, and longer than 3. So, I would like to call vec and Greeks in an automatic way. I would appreciate any hints.

The reason for this is because I will then xtable(M) to put it in my latex code.


Solution

  • If I get it rigth, you can just use paste0 to generate the names

    vec <- c(1.5,1.3,5.7)
    M<-diag(3)
    Greeks <- c("$\\alpha$","$\\beta$","$\\gamma$")
    
    rnames <- paste0(Greeks, " (", vec, ")")
    
    rownames(M) <- rnames
    
    rownames(M)
    
    > [1] "$\\alpha$ (1.5)" "$\\beta$ (1.3)"  "$\\gamma$ (5.7)"