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". For wide data, this can also be a character vector of separate ranking columns such as
c("apple", "orange", "banana").- 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`. For wide data, rankings can be supplied either as a single encoded ranking string such as"123"or as separate ranking columns.- raw
If
TRUE, the function will return the raw average rank. IfFALSE, the function will return the average rank after correcting based on the IPW estimator. Defaults toTRUE.- weight
Optional weight specification. This can be the name of a weight column in
xor a numeric vector with one weight per row. Defaults toNULL.- round
The number of decimal places to round the output to. Defaults to
NULL.
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
x_sep <- data.frame(
apple = c(2, 1, 3),
orange = c(1, 3, 2),
banana = c(3, 2, 1)
)
avg_rank(x_sep, rankings = c("apple", "orange", "banana"))
#> item qoi mean se lower upper method
#> 1 apple Average Rank 2 0.5773503 0.8683935 3.131607 Raw Data
#> 2 orange Average Rank 2 0.5773503 0.8683935 3.131607 Raw Data
#> 3 banana Average Rank 2 0.5773503 0.8683935 3.131607 Raw Data
x_weighted <- data.frame(
rank = c("12", "21"),
survey_weight = c(1, 3)
)
avg_rank(x_weighted, "rank", weight = "survey_weight")
#> item qoi mean se lower upper method
#> 1 1st Average Rank 1.75 0.4330127 -3.751948 7.251948 Raw Data
#> 2 2nd Average Rank 1.25 0.4330127 -4.251948 6.751948 Raw Data
z <- rank_longer(
y,
cols = "rank",
reference = c("Money", "Power", "Respect")
)
#> No ID column specified. Using row number.
#> One column selected. Parsing encoded ranking values.
avg_rank(z, "ranking", items = "item_name", long = TRUE)
#> item_name 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
## 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.6666667 1.0266667 3.640000 Raw Data
#> 2 b Average Rank 1.666667 0.3333333 1.0133333 2.320000 Raw Data
#> 3 c Average Rank 2.000000 0.5773503 0.8683935 3.131607 Raw Data
