无法在 r 中创建函数
Cannot make a function in r
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)
}
}
我正在尝试实现此功能,但出现错误:
错误:找不到对象 'x'
错误:“}”中出现意外的“}”
错误:“}”中出现意外的“}”
我检查了 {} 和 x,但我觉得它不错。
如何创建此功能?
编辑
你需要乘号。 R 不能将括号外的数字理解为乘法。它需要一个乘号 *
例如:
> 5(9+7)
Error: attempt to apply non-function
> 5*(9+7)
[1] 80
我的猜测是,如果您添加乘号,就可以解决问题。我认为您可能还需要修复一些 if else 链,如我在原始答案中所述:
原回答
你的 if 条件后面好像没有加括号。
一般格式为
if(condition you want to test){action you want performed}
您还可以 link 多个测试链一起使用
if(condition 1){action 1} else if(condition 2){action 2}
我不太理解你所做的所有数学运算(抱歉我很懒哈哈),但这里有一个来自你的函数的简化示例,展示了你如何 link 你的测试和操作使用 if还有:
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)
}
以下是当您 运行 函数时会发生的情况:
> 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"
所以在这之后喜欢
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
:
之前,您需要一个右括号 }
,然后是 else
#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)
希望对您有所帮助!
如果没有,这里是别人的博客post:https://www.learnbyexample.org/r-if-else-elseif-statement/
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)
}
}
我正在尝试实现此功能,但出现错误:
错误:找不到对象 'x' 错误:“}”中出现意外的“}” 错误:“}”中出现意外的“}”
我检查了 {} 和 x,但我觉得它不错。 如何创建此功能?
编辑
你需要乘号。 R 不能将括号外的数字理解为乘法。它需要一个乘号 *
例如:
> 5(9+7)
Error: attempt to apply non-function
> 5*(9+7)
[1] 80
我的猜测是,如果您添加乘号,就可以解决问题。我认为您可能还需要修复一些 if else 链,如我在原始答案中所述:
原回答
你的 if 条件后面好像没有加括号。
一般格式为
if(condition you want to test){action you want performed}
您还可以 link 多个测试链一起使用
if(condition 1){action 1} else if(condition 2){action 2}
我不太理解你所做的所有数学运算(抱歉我很懒哈哈),但这里有一个来自你的函数的简化示例,展示了你如何 link 你的测试和操作使用 if还有:
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)
}
以下是当您 运行 函数时会发生的情况:
> 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"
所以在这之后喜欢
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
:
}
,然后是 else
#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)
希望对您有所帮助!
如果没有,这里是别人的博客post:https://www.learnbyexample.org/r-if-else-elseif-statement/