0

I have made some pairwise calculations and stored them in a matrix. Now I would like to label each row and column using some group labels.

name <- c("one name", "two name", "three name", "four name")
group <- c("group a", "group a", "group b", "group b")
groups <- data.frame(names = name, groups = group)

    z <- matrix(c(1,0,0.2,0,
                  0,1,0,1,
                  .02,0.4,1,0,
                  0,.5,0,1),
            nrow=4,
            dimnames=list(name,name))


    z <- as.matrix(z)
    rownames(z) <- groups$groups
    colnames(z) <- groups$groups

When I view the matrix in Rstudio, I see it as:

Why is that? Will it have any effect on my future calculations? What about the spaces that are concatenated into dots and added numbers as suffixes, can I avoid that or change the suffix to specific letters or symbols?

EDIT: Now I as I moved on in my analysis, I've plotted some clusters using the library(factoextra). the fviz_cluster does not allow duplicate row.names, so I guess I'm also looking for a hack that allows that.

Phil
  • 7,287
  • 3
  • 36
  • 66
LRO
  • 79
  • 6

1 Answers1

2

Duplicate row names are permitted in matrices but not data frames. When viewed in RStudio, the matrix is passed to the View() function where it is coerced to a data frame. As part of the coercion the row names are renamed so they are unique and syntactically valid. The original matrix object is, however, unchanged.

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
  • Thank you. The question then becomes, how to change the appearance of those suffixes and concatenations (the actual coercion). – LRO Jun 06 '20 at 13:08
  • How to change how they're displayed in the Viewer? You can't as far as I know. They must be unique and not contain any illegal characters. – Ritchie Sacramento Jun 06 '20 at 13:09
  • It was more the suffix and added dots I was thinking about. I can accept the unique criteria. – LRO Jun 06 '20 at 14:39
  • Given the original row names, what output would you want? R uses `make.names()` to create syntactically valid names for rows and columns - see `make.names(c("group a", "group a", "group b", "group b"))`. – Ritchie Sacramento Jun 06 '20 at 14:47
  • Actually, in my case I think R uses `make.names()` and then `make.unique()`. I was thinking about adding a symbol at the end then, instead of a number or a letter, as letters in my case are deceiving. – LRO Jun 06 '20 at 15:07
  • You are right, it's both. There are few symbols that are legal so you may run into trouble with this approach. – Ritchie Sacramento Jun 06 '20 at 15:12
  • Do you have an idea of how I could make this possible? Maybe through a repeated symbol, such as `c("group.a", "group.a.", "group.a..")`, and how I could realize this? – LRO Jun 06 '20 at 16:33