readpreflib: Read a PrefLib data file

View source: R/readpreflib.R

readpreflibR Documentation

Read a PrefLib data file

Description

Read a file of preferences in the format of PrefLib (www.preflib.org) and return the rankings in the format used by the functions of the package: a matrix with one row per distinct ranking and one column per object, and the frequency of each ranking.

Usage

readpreflib(file, unranked = c("last", "na"), expand = FALSE)

Arguments

file

The name of a PrefLib data file, a URL (for example the raw link of a file of the PrefLib-Data repository on GitHub) or a connection

unranked

What to do with the objects that a judge does not rank, in the files of incomplete orders (soi, toi) and of categorical preferences (cat): "last" (default) ties them in the last position, after the ranked objects, as in the files that PrefLib obtains by adding the unranked alternatives at the bottom; "na" leaves them missing (NA)

expand

Logical. If FALSE (default), the rankings are returned once each with their frequencies in wk; if TRUE, each ranking is repeated as many times as its frequency, one row per judge, and wk is NULL

Details

PrefLib files begin with header lines starting with "#" (file name, title, data type, number of alternatives, names of the alternatives, number of voters, ...), followed by one line for each distinct preference, of the form count: preference. The data types are: strict orders complete (soc), strict orders incomplete (soi), orders with ties complete (toc), orders with ties incomplete (toi) and categorical preferences (cat). In the orders, the alternatives are separated by commas from the first to the last, and tied alternatives are grouped in braces: 13: 1, {4, 3}, 2 means that 13 voters put alternative 1 first, 4 and 3 tied in second position and 2 last. In the categorical preferences each line lists the categories in order, from the most preferred one, and an empty category is {}: the categories become the buckets of a ranking with ties, and the empty categories are skipped. Weighted matching files (wmd) do not contain rankings and are not read.

The function checks that the sum of the frequencies is equal to the number of voters declared in the header. With unranked = "last", two different incomplete orders can give the same ranking (for example the order of all the objects but one, and the full order): such rankings are merged and their frequencies are summed. The rankings are in the order in which they first appear in the file.

With unranked = "na" the result can be used with tau_x and combinpmatr, which treat a pair with a missing object as in Emond and Mason (2002); kemenyd and the median ranking functions need complete rankings, as given by unranked = "last".

Value

A list containing the following components:

X the rankings: a matrix with one row per distinct ranking (one per judge with expand = TRUE) and one column per object, named with the names of the alternatives; the entries are the positions of the objects (1 = first position, objects tied in the same bucket have the same position, the positions of each ranking are 1, 2, 3, ... without gaps)
wk the frequency of each ranking (NULL with expand = TRUE)
datatype the data type of the file: "soc", "soi", "toc", "toi" or "cat"
categories the names of the categories, in order (cat files only, NULL otherwise)
header the metadata of the file: a named list with the values of the header lines

Author(s)

Antonio D'Ambrosio antdambr@unina.it

References

Mattei, N., and Walsh, T. (2013). PrefLib: A library for preferences http://www.preflib.org. In Algorithmic Decision Theory (ADT 2013), Lecture Notes in Computer Science, vol. 8176, 259-270. Springer.

Emond, E. J., and Mason, D. W. (2002). A new rank correlation coefficient with application to the consensus ranking problem. Journal of Multi-Criteria Decision Analysis, 11(1), 17-28.

See Also

consrank, mcbo and consranktopk for the median ranking of the data

tabulaterows for the frequencies of the rankings in a data matrix

Examples

# a small file in the PrefLib format: 4 objects, orders with ties
tf <- tempfile(fileext = ".toi")
writeLines(c(
  "# FILE NAME: example.toi",
  "# TITLE: A small example",
  "# DATA TYPE: toi",
  "# NUMBER ALTERNATIVES: 4",
  "# ALTERNATIVE NAME 1: a",
  "# ALTERNATIVE NAME 2: b",
  "# ALTERNATIVE NAME 3: c",
  "# ALTERNATIVE NAME 4: d",
  "# NUMBER VOTERS: 10",
  "# NUMBER UNIQUE ORDERS: 3",
  "5: 1, {4, 3}, 2",
  "3: 2, 1",
  "2: 3, 1, 2, 4"), tf)
P <- readpreflib(tf)
P$X
P$wk
CR <- consrank(P$X, wk = P$wk, algorithm = "BB")
CR$Consensus
# the objects not ranked by a judge left missing
readpreflib(tf, unranked = "na")$X
## Not run: 
# a file of the PrefLib-Data repository: the sushi data (top-10 of 100 sushi)
P <- readpreflib(paste0("https://raw.githubusercontent.com/PrefLib/",
  "PrefLib-Data/main/datasets/00014%20-%20sushi/00014-00000002.soi"))
dim(P$X)

## End(Not run)


ConsRank documentation built on Nov. 24, 2026, 5:08 p.m.