Skip to contents

This function calculates the average rank for the data frame that contains ranking data. It can be used for both long- and wide-type data frames.

Usage

avg_rank(
  x,
  rankings = NULL,
  items = NULL,
  long = FALSE,
  raw = TRUE,
  weight = NULL,
  round = NULL
)

Arguments

x

A data frame that contains rankings of items.

rankings

The name of the column that contains the rankings. Defaults to NULL, which means that the function will look for a data frame with two columns, "item" and "rank".

items

The name of the column that contains the items' names, or, in case of a wide file, the item names in the reference choice set. Defaults to NULL.

long

The type of the data frame. Defaults to `FALSE`. It `TRUE`, which means that the data frame is in the long format, it is presumed to be generated by rank_longer(). If the data frame is in the wide format, it should be set to `FALSE`. This only accepts a data frame with a single ranking variable that contains rankings, and the rankings should be in the form of a string such as "123".

raw

If TRUE, the function will return the raw average rank. If FALSE, the function will return the average rank after correcting based on the IPW estimator. Defaults to TRUE.

weight

The name of the column that contains the weights for the IPW estimator. Defaults to NULL.

round

The number of decimal places to round the output to. Defaults to NULL.

Value

A data frame with the average rank of each item in the reference choice set.

Examples

x <- data.frame(
  id = c("Bernie", "Yuki", "Silvia"),
  rank = c("123", "321", "213")
)
avg_rank(x, "rank")
#>   item          qoi     mean        se     lower    upper   method
#> 1  1st Average Rank 2.000000 0.5773503 0.8683935 3.131607 Raw Data
#> 2  2nd Average Rank 1.666667 0.3333333 1.0133333 2.320000 Raw Data
#> 3  3rd Average Rank 2.333333 0.6666667 1.0266667 3.640000 Raw Data
avg_rank(x, "rank", items = c("Money", "Power", "Respect"))
#>      item          qoi     mean        se     lower    upper   method
#> 1   Money Average Rank 2.000000 0.5773503 0.8683935 3.131607 Raw Data
#> 2   Power Average Rank 1.666667 0.3333333 1.0133333 2.320000 Raw Data
#> 3 Respect Average Rank 2.333333 0.6666667 1.0266667 3.640000 Raw Data

y <- data.frame(rank = c("123", "321", "213"))
avg_rank(y, "rank")
#>   item          qoi     mean        se     lower    upper   method
#> 1  1st Average Rank 2.000000 0.5773503 0.8683935 3.131607 Raw Data
#> 2  2nd Average Rank 1.666667 0.3333333 1.0133333 2.320000 Raw Data
#> 3  3rd Average Rank 2.333333 0.6666667 1.0266667 3.640000 Raw Data
z <- rank_longer(
  y,
  cols = "rank", id = "id",
  reference = c("Money", "Power", "Respect")
)
#> One column selected. Parsing column by character length.
#> Joining with `by = join_by(item_name)`
avg_rank(z, "ranking", items = "item_name", long = TRUE)
#>   item_name          qoi     mean        se    lower    upper   method
#> 1     Money Average Rank 2.000000 0.3333333 1.346667 2.653333 Raw Data
#> 2     Power Average Rank 1.666667 0.1924501 1.289464 2.043869 Raw Data
#> 3   Respect Average Rank 2.333333 0.3849002 1.578929 3.087738 Raw Data

## Example output from item_to_rank
x <- data.frame(
  item = c("a", "b", "c", "a", "b", "c", "a", "b", "c"),
  rank = c(3L, 1L, 2L, 1L, 2L, 3L, 3L, 2L, 1L)
)
avg_rank(x, long = TRUE)
#>   item          qoi     mean        se    lower    upper   method
#> 1    a Average Rank 2.333333 0.3849002 1.578929 3.087738 Raw Data
#> 2    b Average Rank 1.666667 0.1924501 1.289464 2.043869 Raw Data
#> 3    c Average Rank 2.000000 0.3333333 1.346667 2.653333 Raw Data