OpasnetUtils/Ops

From Opasnet
Jump to: navigation, search



Description

Arithmetic operations of ovariables: first they are merged by index columns, then the operation is performed for the Result.x and Result.y columns.

If one of the expressions is numeric, it is first transformed to ovariable.

Using other functions

result

A problem with non-common functions is that the operations are typically done for the ovariable@output$rescol only, where rescol is the column containing the actual result. For Ops and Math, there is a specifically designed function for performing the operations, but it is not feasible to develop specific S4 functions for all possible functions people want to apply to ovariables.

Instead, we could use a generic function which can be used to squeeze the rescol of interest out of the ovariable in a simple way, apply any functions applicable for vectors, and then assign the outcome back into ovariable. This can be done with result function. result is developed from a part of Ops function.

- Hide code


library(OpasnetUtils)

### result returns a vector that contains the result column of the
### output of a given ovariable. The vector contains the original column
### name as the attribute comment.
### e1 is the ovariable to operate with.

result <- function(e1) { # e1 must be an ovariable or a data.frame.

# Should we allow people to use this for data.frames as well?
#	if(class(e1) == "data.frame") e1 <- new("ovariable", name = character(), output = e1)

	# First check presence of name specific Result-columns
		
	test1 <- "Result" %in% colnames(e1@output)
		
	test3 <- paste(e1@name, "Result", sep = "") %in% colnames(e1@output)
		
	# If found take note
		
	rescol1 <- ifelse(test1, "Result", paste(e1@name, "Result", sep = ""))
		
	if(!(test1 | test3)) stop("No result column found while operating mathematically with ovariables!\n")

	out <- e1@output[[rescol1]]
	comment(out) <- rescol1 # Save the column name for later use
	
	return(out)
}

## "result<-" is a function that tells what is done if content is assigned into Getrescol(ovariable).
## e1 is the ovariable into which something is assigned.
## value is the thing to assign into the ovariable.

assign("result<-", function(e1, value) {
	e1@output[[comment(result(e1))]] <- value
	return(e1)}
)

a <- new("ovariable", 
	name = "a", 
	output = data.frame(A = 1:5, Result = 11:15)
)

result(a)
result(a) <- result(a) * 2
a


cell

Slicing ovariables is a complex thing. This could be done in an easier way by using a function called cell. It does the same thing as e.g. the Cell column in a decision table. It takes in two parameters: variable (the variable name) and cell (conditions to be met). Cell is a string with pairs of column names and locations, separated by colons. If there are several condition pairs, these are separated with semicolons. All conditions must be met.

----#: . What if this is a function that implements the whole decision table including columns Action, Change, and Value? If one only wants to remove rows, the Action is Remove, if select, the Action is Keep? Might be quite useful. Then, the name cell is not very good. --Jouni 06:00, 27 March 2013 (EET) (type: truth; paradigms: science: comment)

Code

https://www.opasnet.org/svn/opasnet_utils/trunk/R/Ops.r

See also