Home > Net >  Cannot make a function in r
Cannot make a function in r

Time:04-16


ICC_calculator <- function(
  beta1,
  beta2,
  beta3,
  ICC,
  one=c('b','w','ratio') 
){
  
  a<-beta1
  b<-beta2
  c<-beta3
  d<-ICC
  
  if(one=="b"){
    if(c==0)
      x <- -((a^2 (d - 1)   b^2 (d - 1)   2 d))/((d (a^2   b^2)))
    return(x)
    
    if(c!=0)
      x1 <- -((sqrt(d (d (a^2   b^2)^2 - 4 c^2 (a^2 (d - 1)   b^2 (d - 1)   2 d)))   a^2 d   b^2 d))/((2 c^2 d))
      x2 <-((sqrt(d (d (a^2   b^2)^2 - 4 c^2 (a^2 (d - 1)   b^2 (d - 1)   2 d)))   a^2 (-d) - b^2 d))/(2 c^2 d)
      
      out<-list(x1, x2)
     return(out)
  }
  if(one=="w"){
    
    x <-((a^2 (-d) - b^2 d - c^2 d - 2 d   1))/((d - 1) (a^2   b^2))
    return(x)
    
  }
  if(one=='ratio'){
    x <-((1 - d (a^2 y   b^2 y   c^2 y^2   2)))/((d - 1) (a^2   b^2)) 
    
    return(x)
  }
}

I am trying to make this function, but it throws errors:

Error: object 'x' not found Error: unexpected '}' in " }" Error: unexpected '}' in "}"

I've checked {}and x, but it seems good to me. How can I create this function?

CodePudding user response:

Edit

You need multiplication signs. R doesn't understand numbers outside of parenthesis as multiplication. It needs a multiplication sign *

For example:

> 5(9 7)
Error: attempt to apply non-function
> 5*(9 7)
[1] 80

My guess is that if you add the multiplication signs, that will fix the problem. I think you may need to also fix some of the if else chains as I described in my original answer:

original answer

It looks like you didn't put brackets after your if condition.

The general format is

if(condition you want to test){action you want performed}

You can also link multiple chains of tests together using

if(condition 1){action 1} else if(condition 2){action 2}

I don't really understand all the math you're doing (sorry I'm lazy haha), but here's a simplified example from your function showing how you can link your tests and actions using if and else:

ICC_calculator <- function(
  beta1,
  one=c('b','w','ratio') 
){
  if(one == "b"){
    x <- beta1*2
  }else if((one=="w")){
    x <- beta1*3
  }else if(one == "ratio"){
    x<- beta1*4
  }else{
    x <- "wheee"
  }
    
  return(x)
}

Here's what happens when you run the function:

> ICC_calculator(beta1 = 2,one = "b")
[1] 4
> ICC_calculator(beta1 = 2,one = "ratio")
[1] 8
> ICC_calculator(beta1 = 2,one = "the user put in something that doesn't make sense")
[1] "wheee"

So like after this

if(one=="b"){
    if(c==0)
      x <- -((a^2 (d - 1)   b^2 (d - 1)   2 d))/((d (a^2   b^2)))
    return(x)

you need a closing bracket } and then and then an else before going into your next if:

#put:
# } else 
# in front of this:
if(c!=0)
      x1 <- -((sqrt(d (d (a^2   b^2)^2 - 4 c^2 (a^2 (d - 1)   b^2 (d - 1)   2 d)))   a^2 d   b^2 d))/((2 c^2 d))
      x2 <-((sqrt(d (d (a^2   b^2)^2 - 4 c^2 (a^2 (d - 1)   b^2 (d - 1)   2 d)))   a^2 (-d) - b^2 d))/(2 c^2 d)
      
      out<-list(x1, x2)
     return(out)

Hope that helps!

If it doesn't, here's someone else's blog post: https://www.learnbyexample.org/r-if-else-elseif-statement/

  • Related