How to use 'hclust' as function call in R How to use 'hclust' as function call in R r r

How to use 'hclust' as function call in R


Do read the help for functions you use. ?hclust is pretty clear that the first argument d is a dissimilarity object, not a matrix:

Arguments:       d: a dissimilarity structure as produced by ‘dist’.

Update

As the OP has now updated their question, what is need is

hclustfunc <- function(x) hclust(x, method="complete")distfunc <- function(x) as.dist((1-cor(t(x)))/2)d <- distfunc(mydata)fit <- hclustfunc(d)

Original

What you want is

hclustfunc <- function(x, method = "complete", dmeth = "euclidean") {        hclust(dist(x, method = dmeth), method = method)}

and then

fit <- hclustfunc(mydata)

works as expected. Note you can now pass in the dissimilarity coefficient method as dmeth and the clustering method.