I tried to construct the clustering method as function the following ways:
mydata <- mtcars
# Here I construct hclust as a function
hclustfunc <- function(x) hclust(as.matrix(x),method="complete")
# Define distance metric
distfunc <- function(x) as.dist((1-cor(t(x)))/2)
# Obtain distance
d <- distfunc(mydata)
# Call that hclust function
fit<-hclustfunc(d)
# Later I'd do
# plot(fit)
But why it gives the following error:
Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") :
missing value where TRUE/FALSE needed
What's the right way to do it?
Do read the help for functions you use.
?hclust
is pretty clear that the first argumentd
is a dissimilarity object, not a matrix:Update
As the OP has now updated their question, what is need is
Original
What you want is
and then
works as expected. Note you can now pass in the dissimilarity coefficient method as
dmeth
and the clustering method.