This function ranks rows in a data frame based on the weighted scores of selected columns. The function normalizes the specified columns (using Min-Max normalization), computes a weighted score for each row, and assigns a rank based on these scores. If no weights are provided, the function defaults to equal weighting for the columns.

rank_results(df, columns_to_maximize, weights = NULL)

Arguments

df

A data frame containing the columns to be ranked.

columns_to_maximize

A character vector of column names that should be maximized and used for ranking.

weights

A numeric vector of weights for the columns. The length of this vector must match the number of columns in columns_to_maximize. Defaults to NULL, in which case equal weights are applied.

Value

A data frame with the original columns, the normalized columns, the weighted score for each row, and the rank based on the weighted score.

Examples

# Create a sample data frame
df <- data.frame(
  A = c(1, 2, 3, 4),
  B = c(2, 3, 4, 5),
  C = c(3, 4, 5, 6)
)

# Specify columns to maximize
columns_to_maximize <- c("A", "B")

# Rank the rows based on the specified columns
ranked_df <- rank_results(df, columns_to_maximize, weights = c(0.7, 0.3))

# View the ranked data frame
print(ranked_df)
#> # A tibble: 4 × 7
#>       A     B     C norm_A norm_B weighted_score  rank
#>   <dbl> <dbl> <dbl>  <dbl>  <dbl>          <dbl> <int>
#> 1     4     5     6  1      1              1         1
#> 2     3     4     5  0.667  0.667          0.667     2
#> 3     2     3     4  0.333  0.333          0.333     3
#> 4     1     2     3  0      0              0         4