【发布时间】:2018-02-28 22:15:18
【问题描述】:
试图在区间 [-1,1] 上绘制以下函数,但出现错误代码:
"Warning messages:
1: In if (g < a) { :
the condition has length > 1 and only the first element will be used
2: In if (g >= a & g <= b) { :
the condition has length > 1 and only the first element will be used"
unifCDF<-function(g) {
if (g< a) {
0
}
else if (g>=a & g<=b) {
(g-a)/(b-a)
}
else if (g>b) {
1
}
}
我知道函数本身有效,因为 unifCDF() 适用于我测试的所有值。有什么想法吗?
【问题讨论】:
-
a和b定义在哪里,你传递给unifCDF()函数的类型是什么? -
a = -1 b = 1,而unifCDF是g的函数
-
if用于将一个值与另一个值进行比较。if(2 > 1) print("yes")- 不用于比较多个值。例如。if(2 > c(1,2,3)) print("yes")发出警告并仅给出第一次比较的结果。 -
我使用了 plot.function(unifCDF,from=a,to=b)
-
"条件的长度 > 1,并且只会使用第一个元素" - 这与我在尝试比较向量而不是单个值时遇到的错误相同。跨度>
标签: r function if-statement plot