简体   繁体   English

R中的ifelse()语句出现问题

[英]Trouble with the ifelse() statement in R

I have a homework assignment where I have to write a Bessel function and compare it to the built in Bessel function in r. 我有一个家庭作业,必须编写一个Bessel函数并将其与r中的内置Bessel函数进行比较。 I have to plot the curve on the interval (0.01:10). 我必须在区间(0.01:10)上​​绘制曲线。 The function has several parts and for the interval 0<=x<=3 I have to find x using equation 1. On the interval 3 < x <= infinity I have to use equation 2. I've tried the if and else but I kept having the "length >1 so first element is used" message. 该函数具有多个部分,对于区间0 <= x <= 3,我必须使用公式1找到x。在区间3 <x <=无穷大时,我必须使用公式2。我尝试了if和else,但是我一直收到“长度> 1,所以使用第一个元素”的消息。 I found out that for vectors I need the ifelse statement but how do I actually use it? 我发现对于向量,我需要ifelse语句,但实际上该如何使用它呢? I played with it and found that it only does true/false type stuff. 我玩过它,发现它只做true / false类型的东西。 ie ifelse(x<=3, y, z) where all the numbers below and equal to x are y and all numbers greater than x are z. 即ifelse(x <= 3,y,z),其中所有小于x且等于x的数字均为y,所有大于x的数字均为z。 How would I go about writing a function where it does equation 1 if x <=3 and equation 2 for all other numbers? 如果x <= 3且所有其他数字的方程式2如何在方程式1处编写函数?

The code I provided is the wrong one and it may be sloppy but I've been playing around with r for one week now and this is about as go 我提供的代码是错误的,可能是草率的,但是我已经在r上玩了一个星期了,这已经可以进行了

x <- seq(.01,10, .01) #sequence of numbers 0.01 - 10 in 0.01 intervals.

#Bessel function for a set of numbers
bess.J = function(x){  
  if(x<=3){
    #
    less3 =  1-2.249997*(x/3)^2+1.2656208*(x/3)^4-0.31638*(x/3)^6+0.044479*  (x/3)^8-0.0039444*(x/3)^10+0.00021*(x/3)^12
    return(less3)
  }
  #
  else{
    Tgreater3 = x - 0.78539816 - 0.04166397*(3/x) - (0.00003954*(3/x)^2) + (0.00262573*(3/x)^3) - (0.00054125*(x/3)^4) - (0.00029333*(3/x)^5) + (0.00013558*(3/x)^6)
    Fgreater3 = 0.79788456 - 0.0000077*(3/x) - (0.00552740*(3/x)^2) - (0.00009512*(3/x)^3) + (0.00137237*(3/x)^4) - (0.00072805*(3/x)^5) + (0.00014476*(3/x)^6)
    Jgreater3 = x^(-1/2)*Fgreater3*cos(Tgreater3)
    return(Jgreater3)
  }
}

plot(x,bess.J(x))

As you said, you can use ifelse() instead of if and else. 如您所说,可以使用ifelse()代替if和else。 I create 2 functions (equation1 and equation2) to make the code more readable. 我创建了2个函数(equation1和equation2),以使代码更具可读性。

equation1 <- function(x){
   1-2.249997*(x/3)^2+1.2656208*(x/3)^4-0.31638*(x/3)^6+0.044479*  (x/3)^8-0.0039444*(x/3)^10+0.00021*(x/3)^12
}

equation2 <- function(x){
  Tgreater3 = x - 0.78539816 - 0.04166397*(3/x) - (0.00003954*(3/x)^2) + (0.00262573*(3/x)^3) - (0.00054125*(x/3)^4) - (0.00029333*(3/x)^5) + (0.00013558*(3/x)^6)
  Fgreater3 = 0.79788456 - 0.0000077*(3/x) - (0.00552740*(3/x)^2) - (0.00009512*(3/x)^3) + (0.00137237*(3/x)^4) - (0.00072805*(3/x)^5) + (0.00014476*(3/x)^6)
  Jgreater3 = x^(-1/2)*Fgreater3*cos(Tgreater3)
  return(Jgreater3)
}

bess.J <- function(x){
  ifelse(x <= 3, equation1(x), equation2(x))
} 

plot(x, bess.J(x))

A possible solution is to write two functions, one for each equation and then use the ifelse to pass the x variable to appropriate equation. 一种可能的解决方案是编写两个函数,每个方程式一个,然后使用ifelse将x变量传递给适当的方程式。
Below I defined the function "eq1" when x<=3 and defined "eq2" for x>3. 下面我在x <= 3时定义函数“ eq1”,并在x> 3时定义“ eq2”。

x <- seq(.01,10, .01) #sequence of numbers 0.01 - 10 in 0.01 intervals.

#Bessel function for a set of number
eq1<- function(x) {
  less3 =  1-2.249997*(x/3)^2+1.2656208*(x/3)^4-0.31638*(x/3)^6+0.044479*  (x/3)^8-0.0039444*(x/3)^10+0.00021*(x/3)^12
  return(less3)
}

eq2<- function(x){
  Tgreater3 = x - 0.78539816 - 0.04166397*(3/x) - (0.00003954*(3/x)^2) + (0.00262573*(3/x)^3) - (0.00054125*(x/3)^4) - (0.00029333*(3/x)^5) + (0.00013558*(3/x)^6)
  Fgreater3 = 0.79788456 - 0.0000077*(3/x) - (0.00552740*(3/x)^2) - (0.00009512*(3/x)^3) + (0.00137237*(3/x)^4) - (0.00072805*(3/x)^5) + (0.00014476*(3/x)^6)
  Jgreater3 = x^(-1/2)*Fgreater3*cos(Tgreater3)
  return(Jgreater3)
}


bess.jx<-ifelse(x<=3, eq1(x), eq2(x))
plot(x,bess.jx)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM