Learn R Programming

tidylearn (version 0.1.0)

tidy_gower: Gower Distance Calculation

Description

Computes Gower distance for mixed data types (numeric, factor, ordered)

Usage

tidy_gower(data, weights = NULL)

Value

A dist object containing Gower distances

Arguments

data

A data frame or tibble

weights

Optional named vector of variable weights (default: equal weights)

Details

Gower distance handles mixed data types:

  • Numeric: range-normalized Manhattan distance

  • Factor/Character: 0 if same, 1 if different

  • Ordered: treated as numeric ranks

Formula: d_ij = sum(w_k * d_ijk) / sum(w_k) where d_ijk is the dissimilarity for variable k between obs i and j

Examples

Run this code
# Create example data with mixed types
car_data <- data.frame(
  horsepower = c(130, 250, 180),
  weight = c(1200, 1650, 1420),
  color = factor(c("red", "black", "blue"))
)

# Compute Gower distance
gower_dist <- tidy_gower(car_data)

Run the code above in your browser using DataLab