Skip to contents

Data Frame to Distance Matrix

Usage

distance_matrix(df = NULL, name_column = NULL, distance = "euclidean")

Arguments

df

(required, data frame) Data frame with numeric columns to transform into a distance matrix. Default: NULL

name_column

(optional, column name) Column naming individual time series. Numeric names are converted to character with the prefix "X". Default: NULL

distance

(optional, character vector) name or abbreviation of the distance method. Valid values are in the columns "names" and "abbreviation" of the dataset distances. Default: "euclidean".

Value

square matrix

See also

Other distances: distance(), distances

Examples

#compute distance matrix
#on data frame with latlong coordinates
m <- distance_matrix(
  df = fagus_coordinates,
  name_column = "name",
  distance = "euclidean"
)

#get data used to compute the matrix
attributes(m)$df
#>           x        y
#> 1 -2.857322 43.17743
#> 2  7.517013 49.32505
#> 3 12.995025 56.40471

#check matrix
m
#>            Spain   Germany    Sweden
#> Spain    0.00000 12.059027 20.646017
#> Germany 12.05903  0.000000  8.951548
#> Sweden  20.64602  8.951548  0.000000
#> attr(,"type")
#> [1] "distance"
#> attr(,"distance")
#> [1] "euclidean"
#> attr(,"df")
#>           x        y
#> 1 -2.857322 43.17743
#> 2  7.517013 49.32505
#> 3 12.995025 56.40471

#compare with sf
# library(sf)
#
# #remove crs to get results in degrees
# sf::st_crs(fagus_coordinates) <- NA
#
# #distance matrix in degrees
# sf::st_distance(
#   x = fagus_coordinates
#   )