Search code examples
rcommandcluster-analysisdbscan

What command returns the number of clusters in dbscan as a value?


I need to a command, similar to length(), to find the number of clusters created in a dbscan.

Suppose I have perform a dbscan on this data set

set.seed(665544)
n <- 600
x <- cbind(runif(10, 0, 10)+rnorm(n, sd=0.2), runif(10, 0, 10)+rnorm(n,sd=0.2))
ds<- dbscan(x,.2,showplot=1)

I can use the command ds to view the number of clusters

ds

Is there a command that I can type and a value for the number of clusters is returned? i.e. I type a command and the value 12 is returned.


Solution

  • If you look on the structure of object ds you will see that there are variable cluster containing cluster number for each observation. 0 in variable cluster is used to code noise observations (see help file of dbscan()).

     str(ds)
    List of 4
     $ cluster: num [1:600] 1 2 3 4 5 10 6 7 8 9 ...
     $ eps    : num 0.2
     $ MinPts : num 5
     $ isseed : logi [1:600] TRUE TRUE TRUE TRUE TRUE FALSE ...
     - attr(*, "class")= chr "dbscan"
    

    To find the number of clusters you can look on maximal value of that variable. Results is 11.

     max(ds$cluster)
    [1] 11