psysky
psysky

Reputation: 3195

keep the observation belonging to the cluster in R

Simple example with iris dataset. I must use apcluster library

library("apcluster")

#use dist() create a negative SimilarityMatrix
sim<-negDistMat(iris[,1:4],r=2)

#run the clusteralgorythm and create apclustert object apiris1
apiris1<-apcluster(sim,details=T)


apiris1=apclusterK(sim,details=T,K=2,verbose=T)

and after, i see the number of cluster and obzervation in it

 Cluster 1, exemplar 8:
      1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 58 99
   Cluster 2, exemplar 124:
      51 52 53 54 55 56 57 59 60 61 62 63 64 65 66 67 68 69 
      70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 
      88 89 90 91 92 93 94 95 96 97 98 100 101 102 103 104 
      105 106 107 108 109 110 111 112 113 114 115 116 117 118 
      119 120 121 122 123 124 125 126 127 128 129 130 131 132 
      133 134 135 136 137 138 139 140 141 142 143 144 145 146 
      147 148 149 150

How to keep the observation belonging to the cluster in R.

To make my post more clear, on output I expect such a table

      n Sepal.Length Sepal.Width Petal.Length Petal.Width    Species Save.cluster
1   1          5.1         3.5          1.4         0.2     setosa            1
2   2          4.9         3.0          1.4         0.2     setosa            1
3   3          4.7         3.2          1.3         0.2     setosa            1
4   4          4.6         3.1          1.5         0.2     setosa            1
5   5          5.0         3.6          1.4         0.2     setosa            1
6   6          5.4         3.9          1.7         0.4     setosa            1
7   7          4.6         3.4          1.4         0.3     setosa            1
8   8          5.0         3.4          1.5         0.2     setosa            1
9   9          4.4         2.9          1.4         0.2     setosa            1
10 10          4.9         3.1          1.5         0.1     setosa            1
11 51          7.0         3.2          4.7         1.4 versicolor            2
12 52          6.4         3.2          4.5         1.5 versicolor            2
13 53          6.9         3.1          4.9         1.5 versicolor            2
14 54          5.5         2.3          4.0         1.3 versicolor            2
15 55          6.5         2.8          4.6         1.5 versicolor            2

Upvotes: 1

Views: 63

Answers (1)

G5W
G5W

Reputation: 37641

The cluster indices are stored in apiris1@clusters. You can make a data.frame like the one you are requesting like this:

iris1 = iris
iris1$Save.cluster = 0
for(i in 1:length(apiris1@clusters)) {
    iris1$Save.cluster[apiris1@clusters[[i]]] = i }
head(iris1)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Save.cluster
1          5.1         3.5          1.4         0.2  setosa            1
2          4.9         3.0          1.4         0.2  setosa            1
3          4.7         3.2          1.3         0.2  setosa            1
4          4.6         3.1          1.5         0.2  setosa            1
5          5.0         3.6          1.4         0.2  setosa            1
6          5.4         3.9          1.7         0.4  setosa            1

Upvotes: 2

Related Questions