plumbr (version 0.6.9)

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

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 DataCamp Workspace