If else statement to check if any numbers are negative in R - r

This is probably very simple, but I am not sure why it's not working.
For input vector b, I want to write a function which begins by checking b for any negative values. If there are any, then the function stops. Otherwise, it continues. What the function is doesn't matter.
Something like this:
F <- function(b) {
if (any(b) < 0) {
warning("error")
} else {
# the function I want to put in
}
}
Edit:
The code that works is
F <- function(b) {
if (any(b < 0)) {
stop("error")
} else {
# the function I want to put in
}
}

Related

If-else statement inside apply function R

I am writing an apply function in R to search a table and return all the instances that TRUE occurs, and I have written the following code but it keeps giving me errors, and I am not sure why. Any help is appreciated.
xsum = apply(genomeTable, 1, function(i) {
if (i) < q.start | if (i) > q.end{
return FALSE
} else{
return TRUE
}
})
sum(xsum)
Your if statement is duplicated. You only need one if for the condition. Parenthesis need to wrap the whole condition. Try this:
xsum = apply(genomeTable, 1, function(i) {
if (i < q.start | i > q.end) {
return(FALSE)
} else {
return(TRUE)
}
})
Try this:
xsum = apply(genomeTable, 1, function(i) ifelse ((i < q.sta | i > q.end), FALSE, TRUE))
It does not work, please provide some data.

R, problems using a for cycle inside if else

I am trying to check if all the elements inside a vector are the same. I am using this code:
if( isTRUE(for(i in 1:length(x)){x[1]==x[i]})){print(x[1])} else{print("several")
Now suppose
x <- c(0,0,0,0,0,0,0,0,0,0,0)
Here, the code should return "0" and if
x <- c(0,0,0,0,0,1,0,0,0,0,0)
it should return "several". In both cases I get "several", any idea why is not working as desired?
Thank u in advance.
there is a simpler way:
if (length(unique(x)) == 1) {
print(x[1])
} else {
print("several")
}
If you want to compare all components of x with the first component you should use all instead of a for loop:
if (all(x == x[1])) {
print(x[1])
} else {
print("several")
}
The result of both approaches is the same.

Three function in R

IS <- function(N,K,sigma,t,r,S_0,a,b,tol){
funct_1 <- function(x){
return((S_0*(exp(-0.5*(sigma^2)*t+sigma*sqrt(t)*x))*(sigma*sqrt(t)-x))+
(exp(-r*t))*K*x)
}
bisection_method <- function(a, b, tol, f = funct_1){
if (f(a)*f(b) > 0){
print("No root found.")
} else
while ((b - a)/2.0 > tol){
midpt= (a + b)/2.0
if (f(midpt) == 0){
return(midpt)
} else if (f(a)*f(midpt) < 0){
b = midpt
} else
a = midpt
}
return(midpt)
}
}
The above function will produce nothing for you. My goal that to input the values of "N,K,sigma,t,r,S_0, a,b" and somehow return "midpt" for me. I have searched a lot but could not come up with anything that makes sense. I have many problems, assume that I input everything things, then how the function "funct_1" will output expression, this expression needs to be recalled to the next function "bisection_method} along with the value of a and b then finally obtain the "midpt" value. Any suggestions are really appreciated. Please let me know if there is anything not clear to you at all.
Your main function doesn't return anything. It just creates the auxiliary functions and then do nothing. That's why you're getting no output.
Try returning the bisection method with appropriate parameters in your main function instead. I also edited so you get NULL output when no root is found.
IS <- function(N,K,sigma,t,r,S_0,a,b,tol){
funct_1 <- function(x){
return((S_0*(exp(-0.5*(sigma^2)*t+sigma*sqrt(t)*x))*(sigma*sqrt(t)-x))+
(exp(-r*t))*K*x)
}
bisection_method <- function(a, b, tol, f = funct_1){
if (f(a)*f(b) > 0){
print("No root found."); return(NULL)
} else
while ((b - a)/2.0 > tol){
midpt= (a + b)/2.0
if (f(midpt) == 0){
return(midpt)
} else if (f(a)*f(midpt) < 0){
b = midpt
} else
a = midpt
}
return(midpt)
}
return(bisection_method(a,b,tol,funct_1))
}
Figured out some parameter combination that makes sense:
IS(1,1,1,4,5,1,.1,9,10^-4)
[1] 2.000023

Test if an unevaluated promise exists

I has the following function:
f <- function(a, b=list()) {
if(exists("b")) {
if(exists("x",b)){
a+b[["x"]]
} else {
a
}
} else {
-a
}
}
And it's work, except when I put a undefined value for b:
exists("tmp")
# [1] FALSE
f(a = 1, b=tmp)
# Error in exists("x", b) : object 'tmp' not found
Is there a function to check if the promise value exists inside my function f?
What about something like this.
f <- function(a, b=list()) {
tryCatch(force(b), error=function(e) b<<-NULL)
if(!is.null(b)) {
if(exists("x",b)){
a+b[["x"]]
} else {
a
}
} else {
-a
}
}
f(5, tmp)
# [1] -5
f(5, list(x=3))
# [1] 8
f(5, list(z=3))
# [1] 5
Here we force evaluation of the parameter to see if it exists or resolves to something within a tryCatch() expression to handle the case where the variable does not exist. If it doesn't exist, we set it to NULL to make the rest of the function easier to work with.

Putting a condition in a for loop

Let's say
i = 1:2029
j = c(3,6,3,5,123,323,423,652,743,885,932)
for loop generally looks like
for (x in i) { }
in here, I would like to calculate the function for x that are not same as j
so it would be something like
for (x in i & !j) { }
but obviously, this kind of command doesn't work.
Is there are command that expresses my intention?
You can use :
for (x in i[-which(i %in% j)]) { }

Resources