In the documentation for dist function in R there's the following words :
method the distance measure to be used. This must be one of "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski". Any unambiguous substring can be given.
But I need to compute the distance based on a customized function that is not in the list, is there a way to specify it ? Or are there other ways other than the dist function that is suitable for my situation ?
I know I can do it in the lapply way, but I'm seeking for a neater way to do it.
Thanks.
Edit:
Th distance method I'm using is a correlation score using pearson distance. Is there a convient way for that ?
Definition: The dist R function computes distance matrices. Basic R Syntax: You can find the basic R programming syntax of the dist function below. dist ( x ) # Basic R syntax of dist function
Compared to the standard dist () function, it supports correlation-based distance measures including "pearson", "kendall" and "spearman" methods. get_dist (x, method = "euclidean", stand = FALSE, ...) fviz_dist ( dist.obj, order = TRUE, show_labels = TRUE, lab_size = NULL, gradient = list (low = "red", mid = "white", high = "blue") )
as.dist () is a generic function. Its default method handles objects inheriting from class "dist", or coercible to matrices using as.matrix (). Support for classes representing distances (also known as dissimilarities) can be added by providing an as.matrix () or, more directly, an as.dist method for such a class.
The dist () function in R can be used to calculate a distance matrix, which displays the distances between the rows of a matrix or data frame. x: The name of the matrix or data frame. method: The distance measure to use. Default is “euclidean” but options include “maximum”, “manhattan”, “canberra”, “binary” or “minkowski”.
You can't; dist()
only knows how to compute certain distances. There are a good number of other distance/dissimilarity coefficients available in R packages via CRAN (including vegan, analogue and the proxy package, amongst many others.)
The latter, proxy, may be particularly suited to your needs. Not only does it have a large number of pre-specified coefficients, it provides a framework for specifying your own distance function that is then called from compiled code so is reasonably fast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With