我试图在R中循环2条曲线,但只有一条曲线显示。更重要的是,我两次调用axis()
命令(对于x和y轴)但是我得到以下错误:
Error in axis(1, at = success/trials) : no locations are finite
'to' must be a finite number
我想知道(a)为什么只有我的第一条曲线显示而(b)为什么我得到Inf/-Inf
我的axis()
来电了吗?
这是我的R代码:
success = c(10, 50) ; trials = 100
for(i in 1:length(success)){
success = success[i]
col = (1:length(success))[i]
c = curve( dbinom(success, trials, x), 0, 1, add = ifelse(i > 1, T, F), ty = "l", xlim = c(0, 1),
col = col, yaxt = "n")
text(success/trials, max(c$y), bquote(bolditalic(H[.(i)])), pos = 3, xpd = T )
axis(1, at = success/trials)
}
axis(2, at = seq(0, max(c$y), len = 7), labels = round(seq(0, max(c$y), len = 7), 2), las = 1 )
答案 0 :(得分:1)
在第一次通过循环时,您将success[1]
设置为success
,然后在第二次循环中将success[2]
设置为success
,但NA
当时长度为1,因此变为col
。
同样,第二个情节没有显示,因为NA
因同样的原因而成为successes = c(10,50)
。
要解决此问题,请在循环中使用不同的名称作为成功列表和变量。例如,使用successes
,然后在循环内设置success
和col
时使用col
。 (或者col = i
,只需设置successes = c(10, 50) ; trials = 100
for(i in 1:length(successes)){
success = successes[i]
col = i
c = curve( dbinom(success, trials, x), 0, 1, add = ifelse(i > 1, T, F), ty = "l", xlim = c(0, 1),
col = col, yaxt = "n")
text(success/trials, max(c$y), bquote(bolditalic(H[.(i)])), pos = 3, xpd = T )
axis(1, at = success/trials)
}
axis(2, at = seq(0, max(c$y), len = 7), labels = round(seq(0, max(c$y), len = 7), 2), las = 1 )
。)
将其应用于您的代码:
DataTable