2016-11-16 31 views
0

我在進一步在代碼x中運行此公式時遇到問題。總之,我試圖通過forloop來模擬這種情況發生1000次,希望能夠增加TF的真實時間量和TS是真實的。我收到錯誤,我錯過了一個TRUE/FALSE語句,並嘗試重新執行該功能,我仍然很困難。任何幫助或建議將不勝感激。爲什麼不按照預期在我的for循環工作中引用x?

#Parameters 

c=0.10 #colonization rate 

A=10 #Area of all islands(km^2) 

d=100 #Distance from host to target (A-T) 

s=0.5 #magnitude of distance 

d0=100 #Specific "half distance" for dispersal(km) 

C1 = c*A*exp(-d/d0) #Mainland to Target colonization 

TS=1 #Target Success 

TF=0 #Target Failure 

Z =runif(1,0,1) 

x <- C1*A 

for(i in 1:1000) 

    if(x[i] <= Z) 

    print("TS") 

    if(x[i] >= Z) 

    print("TF") 

回答

1

的問題是,x僅有1標量值,但你索引它,好像它有1000元。

x 

[1] 3.678794

根據您的描述,它聽起來就像你只是想運行代碼1000次。這確實是:

for(i in 1:1000) { 

c=0.10 #colonization rate 

A=10 #Area of all islands(km^2) 

d=100 #Distance from host to target (A-T) 

s=0.5 #magnitude of distance 

d0=100 #Specific "half distance" for dispersal(km) 

C1 = c*A*exp(-d/d0) #Mainland to Target colonization 

TS=1 #Target Success 

TF=0 #Target Failure 

Z =runif(1,0,1) 

x <- C1*A 

if(x <= Z) { 
    print("TS") 
    } 
if(x >= Z){ 
    print("TF") 
} 
} 
+0

是的,正是我想要做的。感謝一百萬Hack-R –

1

隨着你寫代碼的方式「X」不是一個數組而是一個值,所以你不能解引用的方式你現在做。

相關問題