Convert a set of preferences to a list of choices, alternatives, and preferences.
Arguments
- preferences
a
preferences
object, or an object that can be coerced byas.preferences
.- names
logical: if
TRUE
use the object names in the returnedchoices
object, else use object indices.
Value
A data frame of class choices
with elements:
- choices
A list where each element represents the items chosen for a single rank in the ordering.
- alternatives
A list where each element represents the alternatives (i.e. the set of remaining items to choose from) for a single rank.
- ordering
A list where each element represents the ordering that the choice belongs to.
The list stores the number of choices and the names of the objects as the
attributes nchoices
and objects
respectively.
Examples
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 <- preferences(R, format = "ranking")
actual_choices <- choices(R, names = TRUE)
actual_choices[1:6, ]
#> Preference Set: 1
#> --------------
#> {orange, pear} from {apple, banana, orange, pear}
#> {apple} from {apple, banana}
#> {banana} from {banana}
#> ==============
#> Preference Set: 2
#> --------------
#> {banana} from {apple, banana, orange, pear}
#> {orange} from {apple, orange, pear}
#> {pear} from {apple, pear}
#> ==============
coded_choices <- choices(R, names = FALSE)
coded_choices[1:2, ]
#> Preference Set: 1
#> --------------
#> {3, 4} from {1, 2, 3, 4}
#> {1} from {1, 2}
#> ==============
as.data.frame(coded_choices)[1:2, ]
#> choices alternatives ordering
#> 1 3, 4 1, 2, 3, 4 1
#> 2 1 1, 2 1
attr(coded_choices, "objects")
#> [1] "apple" "banana" "orange" "pear"