Argument is of length zero in if statement using R - r

Does anyone know how I can solve this problem?
for (x in 1:nrow(homer1)-1) {if ((homer1$Start [x+1] +1) == homer1$End [x]) {homer1$annnot_prom <- paste(homer1$Detailed.Annotation, homer1$Nearest.PromoterID, sep="_") } else {homer1$annnot_prom <- homer1$Detailed.Annotation} }
Error in if ((homer1$Start[x + 1] + 1) == homer1$End[x]) { :
argument is of length zero

Add some brackets to your for loop:
for (x in 1:(nrow(homer1)-1))

Related

curve() using a function with argument in R

ppt=function(v, tail = 0.5){
if (tail == 1){
6/pi^2/v^2
} else {
if (v < 11) {
(1-tail)*(11-v)/55
} else {tail*6/pi^2/(v-10)^2}
}
}
curve(ppt(tail = 0.2))
Error in curve(ppt(tail = 0.2)) :
'expr' must be a function, or a call or an expression containing 'x'
How should I plot a smooth curve for the function ppt() with different values of variable tail?
Thank you.
Either (1) rewrite ppt to vectorize it (not shown) or else (2) use Vectorize as shown below in which case ppt does not have to be modified. Also use this syntax.
curve(Vectorize(ppt)(x, tail = 0.2), ylab = "ppt")
Maybe the following trick?
w = 0.2
g <- function(z) ppt(z, tail = w)
curve(g)
And do not forget to replace, as #MrFlick points out in the comment:
if (v < 11) {
(1-tail)*(11-v)/55
} else {tail*6/pi^2/(v-10)^2}
by
ifelse(v < 11, (1-tail)*(11-v)/55, tail*6/pi^2/(v-10)^2)

replacement has length zero in fibonacci sequence in R code

So I have this code for the fibonacci sequence, and I keep getting an error when I try to print out the value of the function.
fibonacci <- function(nn) {
if (!(nn%%1==0) | (nn<1)){
return(0)
}
my.fib <- c(1,1)
for (kk in 3:nn){
my.fib[kk] <- my.fib[kk-1] + my.fib[kk-2]
}
return(my.fib[nn])
}
fibonacci(7)
fibonacci(5)
fibonacci(1)
fibonacci(1.5)
fibonacci(0)
It prints everything correctly for 7,5,1.5 and 0, as it gives me the vaules 13, 5, 0, and 0. But when trying to print fibonacci(1), I get the error
Error in my.fib[kk] <- my.fib[kk - 1] + my.fib[kk - 2] :
replacement has length zero
I want to leave as much as the code the same as possible.
Add another if condition to check for nn = 1.
fibonacci <- function(nn) {
if (!(nn%%1==0) | (nn<1)){
return(0)
} else if(nn == 1) return(1)
my.fib <- c(1,1)
for (kk in 3:nn){
my.fib[kk] <- my.fib[kk-1] + my.fib[kk-2]
}
return(my.fib[nn])
}

R: How to use current function output's within this function

I write a R function using if & else if in it. See the code below:
i_ti_12 <- function(x){
if (x <= 44)
{ti = exp(-13.2238 + 0.152568*x)}
else if (x >= 49)
{ti = -0.01245109 + 0.000315605*x}
else (x > 44 & x < 49)
{ti = (x-44)*(i_ti_12(49))/(49-44) + (49-x)*(i_ti_12(44))/(49-44)}
return(ti)
}
I want to use this function's output, i_ti_12(49) within this function, but the above code doesn't work. The output is:
> i_ti_12(49)
Error: C stack usage 7974292 is too close to the limit
The simple solution is just replace i_ti_12(49) by -0.01245109 + 0.000315605*49, but its not a clear way to solve it and might not work in complex cases.
So I really want to know and to learn if there are clear methods to do this? I mean, like above simple example, write a conditional function using one condition's output in this function. Any help is highly appreciate.
Your last else is followed by a condition (x > 44 & x < 49), which actually is not correct. If you have (x > 44 & x < 49) there, that means you will execute that statement, and ti = (x-44)*(i_ti_12(49))/(49-44) + (49-x)*(i_ti_12(44))/(49-44) is something independent with your if-else structure.
In that case, when you call i_ti_12(49), your function does not know when the recursion should be terminated since you did not define that case.
You can try the code below:
i_ti_12 <- function(x){
if (x <= 44)
{ti = exp(-13.2238 + 0.152568*x)}
else if (x >= 49)
{ti = -0.01245109 + 0.000315605*x}
else
{ti = (x-44)*(i_ti_12(49))/(49-44) + (49-x)*(i_ti_12(44))/(49-44)}
return(ti)
}
such that
> i_ti_12(49)
[1] 0.003013555

Manually written function doesn't behave the same as the gamma function

So I implemented a function that calculates the value of the gamma function. and when I try to multiply f5(a) with a numeric I receive the error : Error in result * f5(a) : non-numeric argument to binary operator and if I instead use result * gamma(a) which is the predefined function it works just fine. It seems like it won't let me do any arithmetic operation with f5 even though it returns the same result as gamma
f5 <- function(a)
{
f <- function(x)
x^(a-1)*exp(-x)
integrate(f, 0, Inf)
}
f6 <- function(a)
{
if (a < 0)
print("a is negative")
else if (a%%1 == 0)
return (factorial(a-1))
else
{
result <- 1
while (a > 1)
{
result <- result * (a - 1)
a <- a - 1
}
result <- result * f5(a)
result
}
}
gamma(0.3)
f5(0.3)
f6(0.3)
This is because of the class of object that gets returned from f5().
class(f5(0.3))
[1] "integrate"
This is a named list object, and you can call the specific value from it:
names(f5(a))
[1] "value" "abs.error" "subdivisions" "message" "call"
You want the value component. Modifying f6() to the code below makes it work:
f6 <- function(a){
if (a < 0){
print("a is negative")
}else if (a%%1 == 0){
return (factorial(a-1))
}else{
result <- 1
while (a > 1){
result <- result * (a - 1)
a <- a - 1
}
result <- result * f5(a)$value
result
}
}

Error in if (num < 0) { : missing value where TRUE/FALSE needed

y <- as.integer(readline(prompt ="Enter a number: "))
factorial = 1
if (y< 0){
print("Error")
} else if (y== 0)
{
print("1")
} else
{
for(i in 1:y) {
factorial = factorial * i
}
return(factorial)
}
wondering why this is giving:
Error in if (y< 0) { : missing value where TRUE/FALSE needed
is it cause the first line has data type NA_integer?
There are three possible ways to pass values to the if statement.
y <- 1
if (y > 0) print("more")
This one works as expected.
y <- 1:3
if (y > 0) print("ignores all but 1st element")
As the warning message will tell you, only the first element was used to evaluate it. You could use any or all to make this right.
y <- NA
if (y > 0) print("your error")
This case actually gives you your error. I would wager a bet that y is somehow NA. You will probably need to provide a reproducible example (with data and the whole shebang) if you'll want more assistance. Note also that it helps visually structure your code to improve readability.

Resources