| psel {rPref} | R Documentation |
Evaluates a preference on a given data set, i.e., returns the maximal elements of a data set for a given preference order.
psel(df, pref, ...) psel.indices(df, pref, ...) peval(pref, ...)
df |
A data frame or, for a grouped preference selection, a grouped data frame. See below for details. |
pref |
The preference order constructed via |
... |
Additional (optional) parameters for top(-level)-k selections:
|
The difference between the three variants of the preference selection is:
The psel function returns a subset of the data set which are the maxima according to the given preference.
The function psel.indices returns just the row indices of the maxima
(except top-k queries with show_level = TRUE, see top-k preference selection).
Hence psel(df, pref) is equivalent to df[psel.indices(df, pref),] for non-grouped data frames.
Finally, peval does the same like psel, but assumes that p has an associated data frame
which is used for the preference selection.
Consider base_pref to see how base preferences are associated with data sets
or use assoc.df to explicitly associate a preference with a data frame.
For a given top value of k the k best elements and their level values are returned. The level values are determined as follows:
All the maxima of a data set w.r.t. a preference have level 1.
The maxima of the remainder, i.e., the data set without the level 1 maxima, have level 2.
The n-th iteration of "Take the maxima from the remainder" leads to tuples of level n.
By default, psel.indices does not return the level values. By setting show_level = TRUE this function
returns a data frame with the columns '.indices' and '.level'.
Note that, if none of the top-k values {top, at_least, top_level} is set,
then all level values are equal to 1.
By definition, a top-k preference selection is non-deterministic.
A top-1 query of two equivalent tuples (equivalence according to pref)
can return on both of these tuples.
For example, a top=1 preference selection on the tuples (a=1, b=1), (a=1, b=2)
w.r.t. low(a) preference can return either the 'b=1' or the 'b=2' tuple.
On the contrary, a preference selection using at_least is deterministic by adding all tuples having the same level as the worst level
of the corresponding top-k query. This means, the result is filled with all tuples being not worse than the top-k result.
A preference selection with top-level-k returns all tuples having level k or better.
If the top or at_least value is greater than the number of elements in df
(i.e., nrow(df)), or top_level is greater than the highest level in df,
then all elements of df will be returned without further warning.
Using psel it is also possible to perform a preference selection where the maxima are calculated for every group separately.
The groups have to be created with group_by from the dplyr package. The preference selection preserves the grouping, i.e.,
the groups are restored after the preference selection.
For example, if the summarize function from dplyr is applied to
psel(group_by(...), pref), the summarizing is done for the set of maxima of each group.
This can be used to e.g., calculate the number of maxima in each group, see the examples below.
A {top, at_least, top_level} preference selection
is applied to each group separately.
A top=k selection returns the k best tuples for each group.
Hence if there are 3 groups in df, each containing at least 2 elements,
and we have top = 2, then 6 tuples will be returned.
On multi-core machines the preference selection can be run in parallel using a divide-and-conquer approach. Depending on the data set, this may be faster than a single-threaded computation. To activate parallel computation within rPref the following option has to be set:
options(rPref.parallel = TRUE)
If this option is not set, rPref will use single-threaded computation by default.
With the option rPref.parallel.threads the maximum number of threads can be specified.
The default is the number of cores on your machine.
To set the number of threads to the value of 4, use:
options(rPref.parallel.threads = 4)
See complex_pref on how to construct a Skyline preference.
# Skyline and top-k/at-least skyline psel(mtcars, low(mpg) * low(hp)) psel(mtcars, low(mpg) * low(hp), top = 5) psel(mtcars, low(mpg) * low(hp), at_least = 5) # preference with associated data frame and evaluation p <- low(mpg, df = mtcars) * (high(cyl) & high(gear)) peval(p) # visualize the skyline in a plot sky1 <- psel(mtcars, high(mpg) * high(hp)) plot(mtcars$mpg, mtcars$hp) points(sky1$mpg, sky1$hp, lwd=3) # grouped preference with dplyr library(dplyr) psel(group_by(mtcars, cyl), low(mpg)) # return size of each maxima group summarise(psel(group_by(mtcars, cyl), low(mpg)), n())