R rename duplicates col and rownames (subindex)

I would really appreciate it if a good-natured soul could tell me how to do this in R:

For a square matrix with duplicate columns and rows, e.g.

1 1 2 2 2 2 3 1 0.000 0.000 0.048 0.048 0.048 0.048 0.059 1 0.000 0.000 0.048 0.048 0.048 0.048 0.059 2 0.048 0.048 0.000 0.000 0.000 0.000 0.059 2 0.048 0.048 0.000 0.000 0.000 0.000 0.059 2 0.048 0.048 0.000 0.000 0.000 0.000 0.059 2 0.048 0.048 0.000 0.000 0.000 0.000 0.059 3 0.059 0.059 0.059 0.059 0.059 0.059 0.000 

where the same column and row names indicate duplicates, I need to have unique column and row names, while preserving the original and duplicate columns / rows. That is, something like

  1 1a 2 2a 2b 2c 3 1 0.000 0.000 0.048 0.048 0.048 0.048 0.059 1a 0.000 0.000 0.048 0.048 0.048 0.048 0.059 2 0.048 0.048 0.000 0.000 0.000 0.000 0.059 2a 0.048 0.048 0.000 0.000 0.000 0.000 0.059 2b 0.048 0.048 0.000 0.000 0.000 0.000 0.059 2c 0.048 0.048 0.000 0.000 0.000 0.000 0.059 3 0.059 0.059 0.059 0.059 0.059 0.059 0.000 

Thanks in advance

+7
matrix r
source share
1 answer

You can use ?make.unique or ?make.names :

 v <- as.character(c(1, 1, 2, 2, 2, 2, 3)) make.unique(v) # [1] "1" "1.1" "2" "2.1" "2.2" "2.3" "3" 

(You must combine this with rownames and colnames .)

+15
source share

All Articles