3
我有一個簡單的功能的[R繪製包含序列/列表
A<-function(s){
c0=360
c1=60
c2=30
k=10
(8/60)*(c0+(sum(k*(c1+c2*(1:(s-1))))))
}
我嘗試繪圖A,但得到一個錯誤信息,這顯然與1的函數:在(S-1)功能
> plot(A, 2,10)
Error in curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
'expr' did not evaluate to an object of length 'n'
In addition: Warning message:
In 1:(s - 1) : numerical expression has 101 elements: only the first used
它也發生在我的包含循環,EX等功能。 for (i in 1:s)
。我認爲他們有類似的問題。
我可以手動創建一個列表A(2)... A(10)然後繪製它。但肯定有辦法解決它,但我不知道如何。
謝謝。
-------更新:功能與循環---------------------------
C<-function(s, SPP){
selector<-allmeans$spp==SPP ###allmeans is a data matrix
meansofspp<-allmeans[selector,]
result.list<-list()
for (i in 1:s){
deltap<-((meansofspp$p[i+1])-(meansofspp$p[i]))
result.list<-append(result.list, deltap)
}
return(sum(unlist(result.list)))
}
SPP
需要字符串如 「OV」, 「SA」 .....
FYI,基質
>meansofspp<-allmeans[selector,]
>selector<-allmeans$spp=="SA"
>meansofspp<-allmeans[selector,]
>meansofspp
Station spp p Psi se_p se_Psi
11 1 SA 0.06805432 0.8258379 0.04033442 0.02424016
21 2 SA 0.08564783 0.7610201 0.04822488 0.04585892
31 3 SA 0.09324792 0.7400703 0.05057707 0.06107310
41 4 SA 0.10526517 0.6976201 0.05539305 0.08971556
51 5 SA 0.11531421 0.6631891 0.05931863 0.12450045
61 6 SA 0.12277445 0.6415915 0.06208516 0.16334959
71 7 SA 0.12762431 0.6341937 0.06323868 0.19052386
81 8 SA 0.13125741 0.6404024 0.06478704 0.24361789
SA_99_new.p.dot 9 SA 0.13300380 0.6578759 0.06518710 0.28016660
> C(1,"SA")
[1] 0.01759351
> plot(Vectorize(C), 1, 10, "SA")
Error in seq.int(from, to, length.out = n) : 'from' must be finite
In addition: Warning message:
In curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, :
NAs introduced by coercion
我看到,它的工作!非常感謝。 – lamushidi
但它不適用於包含「for(i in 1:s)」的函數,爲什麼? – lamushidi
它應該工作。你能用循環和錯誤信息發佈你的函數嗎? –