I have a simple density function below:
下面是一个简单的密度函数:
dpower <- function(x, b, r){
if ((b <= 0 | r <= 0))
return("Wrong parameters entered!")
else{
density.temp <- (r/b)*(x/b)^(r - 1)
density.temp[which(x >= b | x <= 0)] <- NA
return(density.temp)
}
}
This function returns density corresponding to value x from the specified distribution with parameters b and r. I'd like to set the default value on x that if the user doesn't specify x, the default values passes through. We can simply set dpower <- function(x = x.default, b, r)... however, my default value depends on r and b. How can I do that? suppose the default value for x is:
这个函数返回与参数b和r值对应的值x对应的密度。我想在x上设置默认值,如果用户不指定x,默认值就会通过。我们可以简单地设置dpower <- function(x = x.default, b, r)…但是,我的默认值依赖于r和b,我该怎么做呢?假设x的默认值是:
seq(from = 0.05, to = b, by = 0.001)
Thanks for your help,
谢谢你的帮助,
2 个解决方案
#1
23
dpower <- function(b, r, x = seq(from = 0.05, to = b, by = 0.001))
....
#2
5
You can set the value of X to NULL
and have one of the first lines of your function be
您可以将X的值设为NULL,并拥有函数的第一行。
if(is.null(x))
x <- seq(from = 0.05, to = b, by = 0.001)
#1
23
dpower <- function(b, r, x = seq(from = 0.05, to = b, by = 0.001))
....
#2
5
You can set the value of X to NULL
and have one of the first lines of your function be
您可以将X的值设为NULL,并拥有函数的第一行。
if(is.null(x))
x <- seq(from = 0.05, to = b, by = 0.001)