rankings | R Documentation |
Create a "rankings"
object from data or convert a matrix of rankings
or ordered items to a "rankings"
object.
rankings(data, id, item, rank, aggregate = FALSE, verbose = TRUE, ...)
as.rankings(x, ..., verbose = TRUE)
## Default S3 method:
as.rankings(
x,
input = c("rankings", "orderings"),
freq = NULL,
index = NULL,
aggregate = FALSE,
items = NULL,
labels = NULL,
...,
verbose = TRUE
)
## S3 method for class 'grouped_rankings'
as.rankings(x, ..., verbose = TRUE)
## S3 method for class 'matrix'
as.rankings(
x,
input = c("rankings", "orderings"),
freq = NULL,
index = NULL,
aggregate = FALSE,
items = NULL,
labels = NULL,
...,
verbose = TRUE
)
## S3 method for class 'rankings'
x[i, j, ..., drop = TRUE, as.rankings = TRUE]
## S3 method for class 'rankings'
format(x, width = 40L, ...)
data |
a data frame with columns specified by |
id |
an index of |
item |
an index of |
rank |
an index of |
aggregate |
if |
verbose |
logical; if |
... |
further arguments passed to/from methods. |
x |
for |
input |
for |
freq |
an optional column index (number, character or logical)
specifying a column of |
index |
an optional column index (number, character or logical)
specifying a column of |
items |
for |
labels |
for |
i |
indices specifying rankings to extract, as for |
j |
indices specifying items to extract, as for |
drop |
if |
as.rankings |
if |
width |
the width in number of characters to format each ranking - rankings that are too wide will be truncated. |
Each ranking in the input data will be converted to a dense ranking, which
rank items from 1 (first place) to n_r
(last place). Items not ranked
should have a rank of 0 or NA
. Tied items are given the same rank with no
rank skipped. For example {1, 0, 2, 1}, ranks the first and fourth items in
first place and the third item in second place; the second item is unranked.
Records in data
with missing id
or item
are dropped. Duplicated items
in the rankings are resolved if possible: redundant or inconsistent ranks
are set to NA
. Rankings with only 1 item are set to NA
(rankings with
zero items are automatically treated as NA
). Any issues
causing records to be removed or recoded produce a message if
verbose = TRUE
.
For as.rankings
with input = "orderings"
, unused ranks may be filled with
zeroes for numeric x
or NA
. It is only necessary to have as many columns
as ranks that are used.
The method for [
will return a reduced rankings object by default,
recoding as dense rankings and setting invalid rankings to NA
as necessary.
To extract rows and/or columns of the rankings as a matrix or vector,
set as.rankings = FALSE
, see examples.
By default, a "rankings"
object, which is a
matrix of dense rankings with methods for several generics including
aggregate
, [
, format
, rbind()
and
as.matrix()
.
If the object is created with aggregate = TRUE
, or ranking frequencies are
specified via freq
, the rankings are post-processed to create an
"aggregated_rankings"
object.
If a group index is specified via index
, the (possibly aggregated) rankings
are post-processed to create a "grouped_rankings"
object.
# create rankings from data in long form
# example long form data
x <- data.frame(ranking = c(rep(1:4, each = 4), 5, 5, 5),
letter = c(LETTERS[c(1:3, 3, 1:4, 2:5, 1:2, 1)], NA,
LETTERS[3:5]),
rank = c(4:1, rep(NA, 4), 3:4, NA, NA, 1, 3, 4, 2, 2, 2, 3))
# ranking 1 has different rank for same item, but order of items unambiguous
# all ranks are missing in ranking 2
# some ranks are missing in ranking 3
# ranking 4 has inconsistent ranks for two items and a rank with missing item
# ranking 5 is fine - an example of a tie
split(x, x$ranking)
# fix issues when creating rankings object
rankings(x, id = "ranking", item = "letter", rank = "rank")
# convert existing matrix of rankings
R <- matrix(c(1, 2, 0, 0,
4, 1, 2, 3,
2, 1, 1, 1,
1, 2, 3, 0,
2, 1, 1, 0,
1, 0, 3, 2), nrow = 6, byrow = TRUE)
colnames(R) <- c("apple", "banana", "orange", "pear")
R <- as.rankings(R)
# first three rankings
R[1:3,]
# exclude pear from the rankings
R[, -4]
# extract rankings 2 and 3 as numeric matrix
R[2:3, , as.rankings = FALSE]
# same as
as.matrix(R)[2:3,]
# extract rankings for item 1 as a vector
R[,1, as.rankings = FALSE]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.