Difference between revisions of "OpasnetUtils/Ops"

From Testiwiki
Jump to: navigation, search
(idea of Getrescol)
m
 
(One intermediate revision by one other user not shown)
Line 2: Line 2:
 
[[Category:R tools]]
 
[[Category:R tools]]
 
[[Category:OpasnetUtils]]
 
[[Category:OpasnetUtils]]
 +
[[Category:Contains R code]]
 
{{method|moderator=|stub=Yes}}
 
{{method|moderator=|stub=Yes}}
  
Line 10: Line 11:
  
 
===Using other functions===
 
===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.
 
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 Getrescol function. Getrescol is developed from a part of Ops function.
+
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.
  
 
<rcode showcode="1">
 
<rcode showcode="1">
Line 19: Line 22:
 
library(OpasnetUtils)
 
library(OpasnetUtils)
  
### Getrescol returns a vector that contains the result column of the
+
### result returns a vector that contains the result column of the
 
### output of a given ovariable. The vector contains the original column
 
### output of a given ovariable. The vector contains the original column
 
### name as the attribute comment.
 
### name as the attribute comment.
 
### e1 is the ovariable to operate with.
 
### e1 is the ovariable to operate with.
  
Getrescol <- function(e1) { # e1 must be an ovariable or a data.frame.
+
result <- function(e1) { # e1 must be an ovariable or a data.frame.
  
 
# Should we allow people to use this for data.frames as well?
 
# Should we allow people to use this for data.frames as well?
Line 47: Line 50:
 
}
 
}
  
## "Getrescol<-" is a function that tells what is done if content is assigned into Getrescol(ovariable).
+
## "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.
 
## e1 is the ovariable into which something is assigned.
 
## value is the thing to assign into the ovariable.
 
## value is the thing to assign into the ovariable.
  
assign("Getrescol<-", function(e1, value) {
+
assign("result<-", function(e1, value) {
e1@output[[comment(Getrescol(e1))]] <- value
+
e1@output[[comment(result(e1))]] <- value
 
return(e1)}
 
return(e1)}
 
)
 
)
Line 61: Line 64:
 
)
 
)
  
Getrescol(a)
+
result(a)
Getrescol(a) <- Getrescol(a) * 2
+
result(a) <- result(a) * 2
 
a
 
a
 
</rcode>
 
</rcode>
  
 +
 +
'''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.
 +
 +
{{comment|# |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.|--[[User:Jouni|Jouni]] 06:00, 27 March 2013 (EET)}}
  
 
==Code==
 
==Code==

Latest revision as of 09:52, 26 August 2013



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)

Code

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

See also