writing a if-else-then fucntion - r

I am trying to write a function to call a function from a package, snippets as below:
library(optionstrat)
# sameple detla
# do not run
# calldelta(s,x,sigma,t,r)
# putdelta(s,x,sigma,t,r)
x=10
sigma=0.25
t=0.25
r=0.05
delta<-function(option_type,stock_price) {
if (option_type="c") {
delta<-calldelta(s,x,sigma,t,r)
} else {
delta<-putdelta(s,x,sigma,t,r)
}
}
both calldelta and putdelta are built in functions from optionstrat package, and I would like to write a function so that if option_type="c", then return with a call delta value based on the stock price input. Likewise, if option_type!="c", then return with a put delta value based on the stock price input.
My end goal here is to come up with a function like delta(c,10) then return with a call delta value based on stock price 10. May I know how should I do from here? Thanks.
Update 1:
Now I try working with the below snippets:
x=10
sigma=0.25
t=0.25
r=0.05
stock_delta<-function(option_type,s) {
if (option_type="c") {
delta<-calldelta(s,x,sigma,t,r)
} else {
delta<-putdelta(s,x,sigma,t,r)
}
}
And again, if I define optiontype equals to c & s equals to 10, the same warning msg is returned...
Update 2:
Thanks to #akrun, now the function is created & now I would like to add one more return value from the delta function by adding:
calleval(s,x,sigma,t,r)$Gamma
puteval(s,x,sigma,t,r)$Gamma
The first line will return with the call gamma value & the latter will return with put gamma. May I know how do I string it with the function written previously?

There are multiple issues in the function - 1) function arguments passed should match the arguments to the inner function, 2) = is assignment and == is comparison operator, 3) if the last statement is assigned to an object, it wouldn't print on the console. We may either need to return(delta) or in this case there is no need to create an object delta inside (when the function name is also the same), 4) Passing unquoted argument (c) checks for object name c and even have an additional issue as c is also a function name. Instead, pass a string "c" as is expected in the if condition
delta<-function(option_type,stock_price)
{
if (option_type=="c")
calldelta(stock_price,x,sigma,t,r)
else
putdelta(stock_price,x,sigma,t,r)
}
-testing
> delta("c", 10)
[1] 0.5645439

Related

Kusto: Handling null parameter in user defined funtion

I am writing a custom User defined function in kusto where I want to have some optional parameters to the function which will be used in the "where" clause. I want to dynamically handle this. For example: If the value is present, then my where clause should consider that column filter, else it should not be included in the "where" clause>
Eg (psuedo code where value is not null):
function Calculate(string:test)
{
T | where test == test | order by timestamp
}
Eg (psuedo code where value is null or empty. My final query should look like this):
function Calculate(string:test)
{
T | order by timestamp
}
What is the efficient way to implement this. I will call this function from my c# class.
you can define a function with a default value for its argument, and use the logical or operator and the isempty() function to implement the condition you've described.
for example:
(note: the following is demonstrated using a let statement, but can be applied similarly to stored functions)
let T = range x from 1 to 5 step 1 | project x = tostring(x)
;
let F = (_x: string = "") {
T
| where isempty(_x) or _x == x
| order by x desc
}
;
F("abc")
if you run F() or F("") (i.e. no argument, or an empty string as the argument) - it will return all values 1-5.
if you run F("3") - it will return a single record with the value 3.
if you run F("abc") - it will return no records.

R:how to stored and change the input using the function

I recently study R, but I am not familiar with using the function. I was trying to use a function to modify my initial input like:
x<-c(1,2,3,4,5,5,4)
number_to_words<-function(x){
a<-1
while(a<8){
if(x[a]==1){
as.character(x[a])
x[a]<-"one"
}
else if(x[a]==2){
as.character(x[a])
x[a]<-"two"
}
else if(x[a]==3){
as.character(x[a])
x[a]<-"three"
}
else{
as.character(x[a])
x[a]<-"four"
}
a=a+1
}
return(x)
}
number_to_words(x)
x
but I found out that the value in x hadn't been changed, whether there is a method that I could let each component of x[a] in my function to be modified and stored? like x->f(x)->y but x's value is replaced by y?
The issue you have here is that you are assuming that the function is "call by reference" but R uses "call by value". This basically means that a copy of the arguments you pass into a function is made and it is that copy that you are manipulating inside the function.
The easiest solution in your case is to overwrite x when your function returns
x <- number_to_words(x)

Nargin function in R (number of function inputs)

Goal
I am trying to create a function in R to replicate the functionality of a homonymous MATLAB function which returns the number of arguments that were passed to a function.
Example
Consider the function below:
addme <- function(a, b) {
if (nargin() == 2) {
c <- a + b
} else if (nargin() == 1) {
c <- a + a
} else {
c <- 0
}
return(c)
}
Once the user runs addme(), I want nargin() to basically look at how many parameters were passed―2 (a and b), only 1 (a) or none―and calculate c accordingly.
What I have tried
After spending a lot of time messing around with environments, this is the closest I ever got to a working solution:
nargin <- function() {
length(as.list(match.call(envir = parent.env(environment()))))
}
The problem with this function is that it always returns 0, and the reason why is that I think it's looking at its own environment instead of its parent's (in spite of my attempt of throwing in a parent.env there).
I know I can use missing() and args() inside addme() to achieve the same functionality, but I'll be needing this quite a few other times throughout my project, so wrapping it in a function is definitely something I should try to do.
Question
How can I get nargin() to return the number of arguments that were passed to its parent function?
You could use
nargin <- function() {
if(sys.nframe()<2) stop("must be called from inside a function")
length(as.list(sys.call(-1)))-1
}
Basically you just use sys.call(-1) to go up the call stack to the calling function and get it's call and then count the number of elements and subtract one for the function name itself.

R: Function changing print behavior when returning NULL

This question is only for curiosity. My colleague and I were trying to write a function which returns NULL, but doesn't print it.
Before we found return(invisible(NULL)), I tried return({dummy<-NULL}) which works, but only once. After the first evaluation, the functions starts printing again:
test <- function() {
return({x<-NULL})
}
# no printout
test()
# with printout
test()
# with printout
test()
How does this come about?
I think this is due to some older return handling built into R. There are many return functions, withVisible, invisible, etc. When you return an assignment x<-null inside the return function it will not automatically print. If you want an assignment to print...
test <- function() {
withAutoprint(x<-NULL)
}
# with printout this time
test()
# with printout
test()
# with printout
test()
I think this just may be hard coded into the return function, maybe pulling something from this logic below, just a shot in the dark though.
Source: R Documentation
x <- 1
withVisible(x <- 1) # *$visible is FALSE
x
withVisible(x) # *$visible is TRUE
Again if we do not use an expression and simply return a variable or value inside our return function we get automatic printing. The reason I am guessing it returns on a second call has to do with the fact x was already assigned previously.
EDIT: I found this deep into the documentation on auto printing. "Whether the returned value of a top-level R expression is printed is controlled by the global boolean variable R_Visible. This is set (to true or false) on entry to all primitive and internal functions based on the eval column of the table in file src/main/names.c: the appropriate setting can be extracted by the macro PRIMPRINT."(Source)

R - Writing a function to return binary output using if statement

Good day,
I am a beginner and trying to understand why I am getting the error below.
I am trying to create a function that would return 0 or 1 based on column values in data set.
LT = function(Lost.time) {
For (i in 1:dim(df)) {
if (df$Lost.time > 0) {
x = 1
}
else {
x = 0
}
return(x)
}
}
Error: no function to return from, jumping to top level In addition: Warning
message: In if (df$Lost.time > 0) { : the condition has length > 1 and only
the first element will be used> } Error: unexpected '}' in "}"
There are a couple of mistakes in the code:
R is case sensitive. Use for instead of For.
If you are looping over the entries in df$Lost.time, the individual elements should be addressed within the loop using df$Lost.time[i]. However, a loop is not necessary for this task.
An else statement should not begin on a new line of the code. The parser cannot know that the if statement is not finished after the first block. If the else statement is enclosed in curly braces like in } else { there will be no problem in this sense.
The parameter passed to the function is not suitable. Maybe you could pass df, instead of Lost.time, but it may be necessary to rewrite parts of the function.
The use of 1:dim(df) in the for loop should work, but it will trigger a warning message. It is better to use 1:nrow(df).
Those are syntax problems. However, the main issue is probably what has been addressed in the answer by #TimBiegeleisen: In the loop you are checking for each of the ̀nrow(df) elements of df$Lost.time whether a specific condition is fulfilled. It therefore does not seem to make sense to have a single binary result as return value. The purpose of the function should be clarified before it is implemented.
An alternative to this function could be constructed in a one-liner with ifelse.
It is not clear what you actually want to return in your function. return can only be called once, after which it will return a single value and the function will terminate.
If you want to get a vector which will contain 1 or 0 depending on whether a given row in your data frame has Lost.time > 0, then the following one liner should do the trick:
x <- as.numeric(df$Lost.time > 0)
If loops are used for writing a function indices should be used for each element.
Create a variable(x) in your dataframe, if the statements goes true it prints 1 else 0
LT = function(Lost.time) {
for (i in 1:dim(df)) {
if (as.numeric(df$Lost.time[i]) > 0) {
df$x[i] <- 1
}else{
df$x[i] <- 0
}
}
}

Resources