Learn R Programming

editrules (version 2.0-3)

editmatrix: Create an editmatrix

Description

Transforms a list of R (in)equalities into an edit matrix.

Usage

editmatrix(editrules, normalize = TRUE)

Arguments

editrules
data.frame with (in)equalities written in R syntax, see details for description or alternatively a character or expression with (in)equalities written in R syntax
normalize
logical specifying if all edits should be transformed (see description)

Value

  • an object of class "editmatrix" which is a matrix with extra attributes

Details

Transforms a list of R (in)equalities into an edit matrix with coefficients (A) for each variable, and a constant (b) and operator (ops) for each edit rule.

Each row in the resulting editmatrix represents an linear (in) equality. Each column in the resulting editmatrix represents a variable.

There are three forms of creating an editmatrix:

  1. acharactervector with (in)equalities written in R syntax
  2. aexpressionvector with (in)equalities written in R syntax
  3. adata.framewith three columns:
    • name = acharacterwith the name of each rule
    • edit = acharacterwith (in)equalities written in R syntax
    • description = acharacterdescribing the intention of the rule
    Typically these rules are stored in a external csv file (or database).

The third form is the prefered form, because it allows the documentation of constraints. This may be very useful when the incorrect observations are analyzed. The function editrules creates/extracts the third form, which can be used to store edit rules externally or to recreate an editmatrix later on. Functions as.character and as.expression extract the first and second form.

The matrix is created by retrieving the coefficients of the variables in the equalities. i.e. x == y results in c(x=1, y=-1) and x == y + w results in c(x=1, y=-1, w=-1)

By default the editmatrix is created using the comparison operators (==,<=,>=,<,>) in the edits. If option normalize=TRUE is used all edits are transformed into an A == b, A < b or A

See Also

editrules as.editmatrix

Examples

Run this code
# Using a character vector to define contraints
E <- editmatrix(c("x+3*y==2*z", "x==z"))
print(E)

# Using a expression vector to define contraints
E <- editmatrix(expression(x+3*y==2*z, x==z))
print(E)

# an editmatrix also has a summary method:
summary(E)

# select rows from an editmatrix:
E <- editmatrix(c("x+3*y==2*z", "x >= z"))
E[getOps(E) == "=="]


#Using data.frame to define constraints
E.df <- data.frame(
    name =c("A","B","C"),
    edit = c("x == y",    
            "z + w == y + x",
            "z == y + 2*w"),
    description = c(
            "these variables should be equal","","")

)
print(E.df)

E <- editmatrix(E.df)
print(E)

Run the code above in your browser using DataLab