<p>Try to make your rownames unique first.</p>

<p>So if you have this matrix with duplicate rownames:</p>

<pre><code>m &lt;- replicate(20, rnorm(6)) 
rownames(m) &lt;- c("B", "C", "C", "B", "E1", "E3")
m
         [,1]        [,2]      [,3]      [,4]       [,5]        [,6]       [,7]
B   2.4071997  1.30040528 1.0765500 0.2005440  1.1732384  2.34462132  0.3702172
C  -0.2683534 -0.74607893 0.1611073 0.3145887  0.8208741  1.26575765  0.4205310
C   0.1997264  0.03318396 0.1967151 0.7785350  0.9382146 -0.32475195 -0.9832998
B  -1.2726538  0.22519992 1.1556121 0.7161238  0.5601368  1.02900724 -0.2726709
E1  0.8062722 -1.80899128 0.3936155 0.5724118 -0.8163454  0.04306317 -0.4074828
E3  0.3320706  0.21218846 0.9210126 0.9151837  0.4588621 -1.27784583  1.3292399
         [,8]       [,9]       [,10]      [,11]       [,12]       [,13]
B   0.2305559  0.2964808 -0.01035426 -2.6062776 -0.03143381 -1.48828554
C   0.1622548 -0.6244459 -0.66571655 -0.3529994  1.07540375 -1.58949697
C   0.5984752 -0.6423845 -1.00001700  0.2212160  0.05499773  1.36182121
B  -0.5594747 -0.3570748  1.44098577 -0.4939490 -0.93386684  0.54919399
E1  1.6378047  0.8702920  3.06641490  0.1948452  0.36339348 -1.04694826
E3 -2.2422997  1.0682864  0.14326146 -0.4513848 -0.25819231  0.01364221
        [,14]      [,15]      [,16]      [,17]       [,18]      [,19]
B  -2.0410367  2.4858879  1.0564085 -1.7353230 -0.63024160 -1.2642884
C  -1.2216387  0.5533529 -0.2757584 -0.2119105 -0.68899706  1.0587382
C   0.6401224 -1.5998479  1.4072007 -1.5062900 -0.60161624  0.9781313
B   0.4309208  1.4310142 -0.1702066 -0.7195977  0.37466185  0.2605195
E1  1.2538412  0.3413762 -1.9346107  0.7314531 -0.03105008  0.7341104
E3 -0.3146694 -1.3028086  0.5043235 -0.2568268  0.08336845  1.3097557
        [,20]
B  -1.1245496
C  -0.4552617
C  -1.5498408
B   0.1261818
E1 -0.1180164
E3  2.1035368

# Make them unique with make.names function

rownames(m) &lt;- make.names(rownames(m), unique=T)

m
          [,1]        [,2]      [,3]      [,4]       [,5]        [,6]
B    2.4071997  1.30040528 1.0765500 0.2005440  1.1732384  2.34462132
C   -0.2683534 -0.74607893 0.1611073 0.3145887  0.8208741  1.26575765
C.1  0.1997264  0.03318396 0.1967151 0.7785350  0.9382146 -0.32475195
B.1 -1.2726538  0.22519992 1.1556121 0.7161238  0.5601368  1.02900724
E1   0.8062722 -1.80899128 0.3936155 0.5724118 -0.8163454  0.04306317
E3   0.3320706  0.21218846 0.9210126 0.9151837  0.4588621 -1.27784583
          [,7]       [,8]       [,9]       [,10]      [,11]       [,12]
B    0.3702172  0.2305559  0.2964808 -0.01035426 -2.6062776 -0.03143381
C    0.4205310  0.1622548 -0.6244459 -0.66571655 -0.3529994  1.07540375
C.1 -0.9832998  0.5984752 -0.6423845 -1.00001700  0.2212160  0.05499773
B.1 -0.2726709 -0.5594747 -0.3570748  1.44098577 -0.4939490 -0.93386684
E1  -0.4074828  1.6378047  0.8702920  3.06641490  0.1948452  0.36339348
E3   1.3292399 -2.2422997  1.0682864  0.14326146 -0.4513848 -0.25819231
          [,13]      [,14]      [,15]      [,16]      [,17]       [,18]
B   -1.48828554 -2.0410367  2.4858879  1.0564085 -1.7353230 -0.63024160
C   -1.58949697 -1.2216387  0.5533529 -0.2757584 -0.2119105 -0.68899706
C.1  1.36182121  0.6401224 -1.5998479  1.4072007 -1.5062900 -0.60161624
B.1  0.54919399  0.4309208  1.4310142 -0.1702066 -0.7195977  0.37466185
E1  -1.04694826  1.2538412  0.3413762 -1.9346107  0.7314531 -0.03105008
E3   0.01364221 -0.3146694 -1.3028086  0.5043235 -0.2568268  0.08336845
         [,19]      [,20]
B   -1.2642884 -1.1245496
C    1.0587382 -0.4552617
C.1  0.9781313 -1.5498408
B.1  0.2605195  0.1261818
E1   0.7341104 -0.1180164
E3   1.3097557  2.1035368
</code></pre>

CC BY-SA 3.0
2018-03-19T20:30:27.513Z
2018-03-19T20:30:27.513Z
benn
<p>I have a matrix object in R of 1500 rows and 20 columns. Based on my own algorithm of analysis, I know there are roughly 7 clusters. In my matrix object, each row is one data point, and is labeled to which cluster it belongs by its row name. Thus, there are duplicated row names. I wanted to see if PCA would give me the approximate same clustering of my data.</p>

<pre><code>Segment1 Segment2 Segment3 Segment4 Segment5 Segment6 Segment7 Segment8 Segment9 Segment10 Segment11 Segment12 Segment13 Segment14 Segment15 Segment16 Segment17 Segment18 Segment19 Segment20
B  38.40000 41.75200 44.38400 44.18400 45.37600 37.49600 41.36800 33.93600 38.00800  42.51200 46.49600  40.48000  45.40800  46.32800  43.78400  39.88800  38.84000  40.56800  42.03200 38.89185
C  45.53846 50.08462 39.91538 36.95385 34.96154 39.74615 38.01538 35.75385 35.54615  36.69231 35.20769  38.05385  39.29231  37.96923  37.30000  36.86923  39.19231  38.81538  43.69231 38.06400 
C  46.05176 41.69412 38.80000 37.75529 39.67529 39.07765 39.17647 38.24941 39.58588  38.63529 38.30588  41.87765  38.97412  40.13647  42.27294  38.24471  35.41647  40.80000  38.07059 42.11294
B  44.20000 43.42857 44.80000 35.20000 35.91429 37.82857 51.45714 44.68571 46.68571  48.74286 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
E1 45.06909 41.09818 40.02909 42.50182 42.34909 39.84727 41.42909 40.47273 40.28000  40.51636 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
E3 40.87407 39.27704 44.13630 43.25037 35.86667 37.30667 38.76148 40.74667 38.93333  43.16148 37.47259  37.73630  38.34370  39.00148  36.96889  37.76593  39.14667  37.92593  37.62963 38.89185
</code></pre>

<p>(The first column are the row names, either being A,B,C,D,E1,E2 or E3 according to my clustering)</p>

<p>I used various packages and functions like <strong>pcomp()</strong>, <strong>princomp()</strong> from the <strong>stats</strong> package, and <strong>PCA()</strong> from the <strong>FactoMineR</strong> package. If I wanted to do hierarchical clustering with the <strong>HCPC()</strong> function from the same package, it was not possible because of the duplicated row names. </p>

<p>Using:</p>

<pre><code>fviz_pca_ind(pca1, geom.ind = "point", axes = c(1,3) , col.ind = new$Class, palette = c("#66C2A5" ,"#FC8D62" ,"#8DA0CB", "#E78AC3" ,"#A6D854", "#FFD92F", "#E5C494"), addEllipses = T, legend.title="Classes")
</code></pre>

<p>I got the individual datapoints on my two PC axes with ellipses around them.</p>

<p>But for clustering: </p>

<pre><code>PCA_FR &lt;- PCA(mat_data, ncp = 4, graph = FALSE)
Hier_PCA&lt;- HCPC(PCA_FR, graph = F)
</code></pre>

<p>I couldn't compute Hier_PCA because of duplicated row names. </p>

<pre><code>&gt; Hier_PCA&lt;- HCPC(PCA_FR, graph = F)
Error in `contrasts&lt;-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels
In addition: Warning messages:
1: In data.row.names(row.names, rowsi, i) :
some row.names duplicated:  3,4,7,8,10,11,12,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,
</code></pre>

<p>How can I best cluster my data using the first three PCs, and visualize the 1500 datapoints, but labeled only in 7 ways? </p>

CC BY-SA 3.0
2018-03-19T10:16:08.567Z
Did you tried to use unique rownames and store the name of the cluster each data belongs in another vector? What was then the problem?
llrs
CC BY-SA 3.0
2018-03-19T10:22:19.863Z
I didn't try that because I want to see my clusters according to the labels A,B,C,D,E1,E2,E3. Doing that, I thought I would still see just numbers, and not my labels.
rishi
CC BY-SA 3.0
2018-03-19T10:27:11.223Z
Yes, but once the position are calculated you can use the base function `text` to plot the labels at that positions as previously commented. (Please do not remove the comments so quickly moderator :)
llrs
CC BY-SA 3.0
2018-03-19T10:35:17.687Z
Aah okay. Can you show me the code on how to use the text function and separate my rows according to the rownames, in the answer?
rishi
CC BY-SA 3.0
2018-03-19T10:46:51.407Z
I could in a while, but search in StackOverflow, I'm sure this has been answered somewhere (I  learned from there)
llrs
<r><pca>
Hierarchial PCA Clustering with duplicated row names
# Hierarchial PCA Clustering with duplicated row names

(Asked by: username_0 on 2018-03-19)

I have a matrix object in R of 1500 rows and 20 columns. Based on my own algorithm of analysis, I know there are roughly 7 clusters. In my matrix object, each row is one data point, and is labeled to which cluster it belongs by its row name. Thus, there are duplicated row names. I wanted to see if PCA would give me the approximate same clustering of my data.
Segment1 Segment2 Segment3 Segment4 Segment5 Segment6 Segment7 Segment8 Segment9 Segment10 Segment11 Segment12 Segment13 Segment14 Segment15 Segment16 Segment17 Segment18 Segment19 Segment20
B  38.40000 41.75200 44.38400 44.18400 45.37600 37.49600 41.36800 33.93600 38.00800  42.51200 46.49600  40.48000  45.40800  46.32800  43.78400  39.88800  38.84000  40.56800  42.03200 38.89185
C  45.53846 50.08462 39.91538 36.95385 34.96154 39.74615 38.01538 35.75385 35.54615  36.69231 35.20769  38.05385  39.29231  37.96923  37.30000  36.86923  39.19231  38.81538  43.69231 38.06400 
C  46.05176 41.69412 38.80000 37.75529 39.67529 39.07765 39.17647 38.24941 39.58588  38.63529 38.30588  41.87765  38.97412  40.13647  42.27294  38.24471  35.41647  40.80000  38.07059 42.11294
B  44.20000 43.42857 44.80000 35.20000 35.91429 37.82857 51.45714 44.68571 46.68571  48.74286 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
E1 45.06909 41.09818 40.02909 42.50182 42.34909 39.84727 41.42909 40.47273 40.28000  40.51636 41.25091  39.45455  38.17091  40.70182  40.39273  41.28727  40.63636  41.50909  41.68364 41.29455
E3 40.87407 39.27704 44.13630 43.25037 35.86667 37.30667 38.76148 40.74667 38.93333  43.16148 37.47259  37.73630  38.34370  39.00148  36.96889  37.76593  39.14667  37.92593  37.62963 38.89185

(The first column are the row names, either being A,B,C,D,E1,E2 or E3 according to my clustering)
I used various packages and functions like pcomp(), princomp() from the stats package, and PCA() from the FactoMineR package. If I wanted to do hierarchical clustering with the HCPC() function from the same package, it was not possible because of the duplicated row names. 
Using:
fviz_pca_ind(pca1, geom.ind = "point", axes = c(1,3) , col.ind = new$Class, palette = c("#66C2A5" ,"#FC8D62" ,"#8DA0CB", "#E78AC3" ,"#A6D854", "#FFD92F", "#E5C494"), addEllipses = T, legend.title="Classes")

I got the individual datapoints on my two PC axes with ellipses around them.
But for clustering: 
PCA_FR <- PCA(mat_data, ncp = 4, graph = FALSE)
Hier_PCA<- HCPC(PCA_FR, graph = F)

I couldn't compute Hier_PCA because of duplicated row names. 
> Hier_PCA<- HCPC(PCA_FR, graph = F)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels
In addition: Warning messages:
1: In data.row.names(row.names, rowsi, i) :
some row.names duplicated:  3,4,7,8,10,11,12,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,

How can I best cluster my data using the first three PCs, and visualize the 1500 datapoints, but labeled only in 7 ways?

**Tags:** r, pca
> **Comments:**
> **username_1**: Did you tried to use unique rownames and store the name of the cluster each data belongs in another vector? What was then the problem?  
> **username_0**: I didn't try that because I want to see my clusters according to the labels A,B,C,D,E1,E2,E3. Doing that, I thought I would still see just numbers, and not my labels.  
> **username_1**: Yes, but once the position are calculated you can use the base function `text` to plot the labels at that positions as previously commented. (Please do not remove the comments so quickly moderator :)  
> **username_0**: Aah okay. Can you show me the code on how to use the text function and separate my rows according to the rownames, in the answer?  
> **username_1**: I could in a while, but search in StackOverflow, I'm sure this has been answered somewhere (I  learned from there)  

---

## 1 answers

### Answer by username_2 on 2018-03-19. Score: 2

Try to make your rownames unique first.
So if you have this matrix with duplicate rownames:
m <- replicate(20, rnorm(6)) 
rownames(m) <- c("B", "C", "C", "B", "E1", "E3")
m
         [,1]        [,2]      [,3]      [,4]       [,5]        [,6]       [,7]
B   2.4071997  1.30040528 1.0765500 0.2005440  1.1732384  2.34462132  0.3702172
C  -0.2683534 -0.74607893 0.1611073 0.3145887  0.8208741  1.26575765  0.4205310
C   0.1997264  0.03318396 0.1967151 0.7785350  0.9382146 -0.32475195 -0.9832998
B  -1.2726538  0.22519992 1.1556121 0.7161238  0.5601368  1.02900724 -0.2726709
E1  0.8062722 -1.80899128 0.3936155 0.5724118 -0.8163454  0.04306317 -0.4074828
E3  0.3320706  0.21218846 0.9210126 0.9151837  0.4588621 -1.27784583  1.3292399
         [,8]       [,9]       [,10]      [,11]       [,12]       [,13]
B   0.2305559  0.2964808 -0.01035426 -2.6062776 -0.03143381 -1.48828554
C   0.1622548 -0.6244459 -0.66571655 -0.3529994  1.07540375 -1.58949697
C   0.5984752 -0.6423845 -1.00001700  0.2212160  0.05499773  1.36182121
B  -0.5594747 -0.3570748  1.44098577 -0.4939490 -0.93386684  0.54919399
E1  1.6378047  0.8702920  3.06641490  0.1948452  0.36339348 -1.04694826
E3 -2.2422997  1.0682864  0.14326146 -0.4513848 -0.25819231  0.01364221
        [,14]      [,15]      [,16]      [,17]       [,18]      [,19]
B  -2.0410367  2.4858879  1.0564085 -1.7353230 -0.63024160 -1.2642884
C  -1.2216387  0.5533529 -0.2757584 -0.2119105 -0.68899706  1.0587382
C   0.6401224 -1.5998479  1.4072007 -1.5062900 -0.60161624  0.9781313
B   0.4309208  1.4310142 -0.1702066 -0.7195977  0.37466185  0.2605195
E1  1.2538412  0.3413762 -1.9346107  0.7314531 -0.03105008  0.7341104
E3 -0.3146694 -1.3028086  0.5043235 -0.2568268  0.08336845  1.3097557
        [,20]
B  -1.1245496
C  -0.4552617
C  -1.5498408
B   0.1261818
E1 -0.1180164
E3  2.1035368

# Make them unique with make.names function

rownames(m) <- make.names(rownames(m), unique=T)

m
          [,1]        [,2]      [,3]      [,4]       [,5]        [,6]
B    2.4071997  1.30040528 1.0765500 0.2005440  1.1732384  2.34462132
C   -0.2683534 -0.74607893 0.1611073 0.3145887  0.8208741  1.26575765
C.1  0.1997264  0.03318396 0.1967151 0.7785350  0.9382146 -0.32475195
B.1 -1.2726538  0.22519992 1.1556121 0.7161238  0.5601368  1.02900724
E1   0.8062722 -1.80899128 0.3936155 0.5724118 -0.8163454  0.04306317
E3   0.3320706  0.21218846 0.9210126 0.9151837  0.4588621 -1.27784583
          [,7]       [,8]       [,9]       [,10]      [,11]       [,12]
B    0.3702172  0.2305559  0.2964808 -0.01035426 -2.6062776 -0.03143381
C    0.4205310  0.1622548 -0.6244459 -0.66571655 -0.3529994  1.07540375
C.1 -0.9832998  0.5984752 -0.6423845 -1.00001700  0.2212160  0.05499773
B.1 -0.2726709 -0.5594747 -0.3570748  1.44098577 -0.4939490 -0.93386684
E1  -0.4074828  1.6378047  0.8702920  3.06641490  0.1948452  0.36339348
E3   1.3292399 -2.2422997  1.0682864  0.14326146 -0.4513848 -0.25819231
          [,13]      [,14]      [,15]      [,16]      [,17]       [,18]
B   -1.48828554 -2.0410367  2.4858879  1.0564085 -1.7353230 -0.63024160
C   -1.58949697 -1.2216387  0.5533529 -0.2757584 -0.2119105 -0.68899706
C.1  1.36182121  0.6401224 -1.5998479  1.4072007 -1.5062900 -0.60161624
B.1  0.54919399  0.4309208  1.4310142 -0.1702066 -0.7195977  0.37466185
E1  -1.04694826  1.2538412  0.3413762 -1.9346107  0.7314531 -0.03105008
E3   0.01364221 -0.3146694 -1.3028086  0.5043235 -0.2568268  0.08336845
         [,19]      [,20]
B   -1.2642884 -1.1245496
C    1.0587382 -0.4552617
C.1  0.9781313 -1.5498408
B.1  0.2605195  0.1261818
E1   0.7341104 -0.1180164
E3   1.3097557  2.1035368


---

