Learn R Programming

plumbr (version 0.6.10)

ItemSelection-class: The ItemSelection class implements '>Selection for the very common case of selecting items in a dataset, optionally with weights.

Description

The ItemSelection class implements '>Selection for the very common case of selecting items in a dataset, optionally with weights.

Arguments

Constructor

  • ItemSelection(delegate = NULL): Constructs an ItemSelection object with the underlying selection provided by delegate, which may be a function or any other R object. If it is not a function, delegate must support the coercions described in the next section. A good example would be a logical vector. However, delegate is usually a function that is invoked whenever the selection is stored or retrieved. If the function is called with no arguments, it should return the selection. Otherwise, the argument is the new selection status, and the function should store it. This is the same semantic as active bindings. This dynamic functionality allows proxying of other Selection objects or external sources, such as a selection model from a GUI toolkit.

Interpreting the Selection

Any R object can represent the underlying selection, so for simplicity we recommend that the client interpret the selection through coercion. Each of these simply delegate to the underlying selection object, which will need to support all of them for consistency. The following coercions are supported, where x is a ItemSelection instance:

  • which(x): integer indices of the selected items.

  • as.logical(x): TRUE where selected.

  • as.integer(x): usually 0L (unselected) or 1L (selected), but in general it is a weighting of the selection.

  • as.numeric(x): similar to as.integer, except with real values.

  • as.factor(x): ordinarily this will have two levels, FALSE and TRUE, although it could have more, which confers support for multinary selections.

Supported Selection Calculus

All operations mentioned in '>Selection are supported: add, subtract, toggle, intersect.

See Also

'>Selection for the rest of the details.

Examples

Run this code
# NOT RUN {
  ## Assume we have a dataset:
  data(Cars93, package="MASS")
  mf <- mutaframe(Cars93)
  mf$.color <- "gray"
  ## First step is to create a base selection
  sel <- ItemSelection()
  ## Now, link that selection to other cases in same dataset by some variable
  linked_sel <- sel$link(match_any_linker(Cars93["Manufacturer"]))
  ## Finally, scale that linked selection to the data
  linked_sel$scale(function(x, d) {
    d[as.logical(x), ".color"] <- "red"
  }, mf)
  ## To test, select some cases
  cases <- rep(FALSE, nrow(mf))
  cases[seq(1, 10, 2)] <- TRUE
  sel$replace(cases)
# }

Run the code above in your browser using DataLab