Skip to contents

Create an object of class grouped_preferences which associates a group index with an object of class preferences. This allows the preferences to be linked to covariates with group-specific values.

Usage

group(x, ...)

# S3 method for preferences
group(x, index, ...)

# S3 method for grouped_preferences
[(x, i, j, ...)

# S3 method for grouped_preferences
format(x, max = 2L, width = 20L, ...)

Arguments

x

A preferences object for group(); otherwise a grouped_preferences object.

...

Additional arguments passed on to as.preferences by grouped_preferences; unused by format.

index

A numeric vector or a factor with length equal to the number of preferences specifying the subject for each set.

i

Indices specifying groups to extract, may be any data type accepted by [.

j

Indices specifying items to extract. object, otherwise return a matrix/vector.

max

The maximum number of preferences to format per subject.

width

The maximum width in number of characters to format the preferences.

Value

An object of class grouped_preferences, which is a vector of of group IDs with the following attributes:

preferences

The preferences object.

index

An index matching each preference set to each group ID.

Examples


# ungrouped preferences (5 preference sets, 4 items)
R <- as.preferences(
  matrix(c(
    1, 2, 0, 0,
    0, 2, 1, 0,
    0, 0, 1, 2,
    2, 1, 0, 0,
    0, 1, 2, 3
  ), ncol = 4, byrow = TRUE),
  format = "ranking",
  item_names = LETTERS[1:4]
)
length(R)
#> [1] 5

# group preferences (first three in group 1, next two in group 2)
G <- group(R, c(1, 1, 1, 2, 2))
length(G)
#> [1] 2

## by default up to 2 preference sets are shown per group, "..." indicates if
## there are further preferences
G
#>                                     1                                     2 
#> [C = D > A > B], [A = D > C > B], ...      [C = D > B > A], [A > B > C > D] 
print(G, max = 1)
#>                    1                    2 
#> [C = D > A > B], ... [C = D > B > A], ... 

## select preferences from group 1
G[1, ]
#>                                     1 
#> [C = D > A > B], [A = D > C > B], ... 

## exclude item 3 from preferences
G[, -3]
#>                             1                             2 
#> [D > A > B], [A = D > B], ...      [D > B > A], [A > B > D] 

## Project preferences in all groups to their first preference
G[, 1, by.rank = TRUE]
#>                     1                     2 
#> [C = D], [A = D], ...          [C = D], [A] 

## preferences from group 2, excluding item 3
## - note group 2 becomes the first (and only) group
G[2, -3]
#>                        1 
#> [D > B > A], [A > B > D] 

# Group preferences by a factor
G <- group(R, factor(c("G1", "G1", "G1", "G2", "G2")))

G
#>                                    G1                                    G2 
#> [C = D > A > B], [A = D > C > B], ...      [C = D > B > A], [A > B > C > D] 
print(G, max = 1)
#>                   G1                   G2 
#> [C = D > A > B], ... [C = D > B > A], ... 

## select preferences from group G1
G["G1"]
#>                                    G1 
#> [C = D > A > B], [A = D > C > B], ...