2013-02-02 24 views
0

我要獲得向量simualted_results以獲取由「simulation」返回的值,該值根據迭代生成了長度不定的向量。函數不填充R中的空向量

起初,我有這個代碼的工作,但速度很慢:

simulated_results<-NULL 
while(as.numeric(Sys.time())-start<duration){ 
    simulated_results <- cbind(simulated_results,simulation(J,4* (length(J)^2),0.0007,duration,start)) 

} 

但它的速度很慢,所以我修改了它:

start<-as.numeric(Sys.time()) 
duration<-10 
simulated_results<-NULL 
simulated_results <- cbind(simulated_results, 
         replicate(n=10000,expr=(while(as.numeric(Sys.time())-start<duration) 
         {simulation(J,4*(length(J)^2),0.0007,duration,start)}))) 

現在有了新的代碼,我的問題是,儘管一切正常運行,我不能得到模擬結果傳遞給simualted_results,而是simualted_results jsut採用NULL值的列向量 我得到沒有錯誤信息

我將不勝感激任何幫助!

參考模擬代碼:

iter<-as.numeric(Sys.getenv("PBS_ARRAY_INDEX")) 

if(iter <= 40){J<-1:500 
}else if(iter <= 80){J<-1:1500 
}else if(iter <= 120){J<-1:2500 
}else if(iter <= 160){J<-1:5000} 

set.seed(iter) 
simulation <- function(J,gens,v=0.1,duration,start){ 

    species_richness <- function(J){ 
    a <- table(J) 
    return(NROW(a)) 
    } 

    start<-as.numeric(Sys.time()) 

    species_richness_output <- rep(NA,gens) 
    for(rep in 1:gens){ 

    if (as.numeric(Sys.time())-start<duration){ 

     index1 <- sample(1:length(J),1) 

     if(runif(1,0,1) < v){ 
     J[index1] <- (rep+100) 
     } 
     else{ 
     index2 <- sample(1:length(J),1) 
     while(index1==index2) { 
      index2 <- sample(1:length(J),1) 
     } 
     J[index1] <- J[index2] 
     } 
     species_richness_output[rep] <- species_richness(J)} else break 
    } 

    species_abundance <- function(J){ 
    a <- table(J) 
    return(a) 
    } 

    abuntable <- species_abundance(J) 

    octaves <- function(abuntable) 
    { 
    oct<-rep(0,floor(log2(length(J))+1)) 

    for(i in 1:length(abuntable)){ 
     oct2 <- floor(log2(abuntable[i])+1) 
     oct[oct2] <- oct[oct2]+1 
    } 

    return(oct) 
    } 

    octaves(abuntable) 
} 
+1

這裏有很多問題。你的代碼混亂不清,我們沒有'simulation()'函數,即使我們做了,它看起來像你使用組合cbind()。 .replicate().. simulation()非常不正確。即使是描述問題的段落也是不連貫的。如果沒有清楚地描述數據以及如何操縱數據,您不可能獲得任何幫助。 – N8TRO

+0

您也可以使用您的舊代碼,並預先分配您的結果所需的數據結構。緩慢來自內存不斷重新分配,隨着對象的增長。 –

+0

我想我們需要看看'simulation()'知道如何幫助你。在全球環境中「J」嗎? –

回答

1

我@Nathan摹同意的事,但沒引起我的注意:您正在嘗試cbind兩件事不能被綁定在一起,因爲他們有不同的尺寸。我們不知道你的simulation函數返回什麼樣的數據類型,但它顯然不是NULL。考慮到這一點:

df1 <- NULL 
df2 <- data.frame(x = 1:10, y = 11:20) 
cbind(df1, df2) 
cbind(df2, df1) 

這兩個cbind語句都會給出錯誤。你有錯誤嗎?如果發生這種情況,則應將simulated_results初始化爲NULL而不是simulation函數返回的空白版本。

編輯

iter = 10 
set.seed(iter) 
J <- 1:1500 
# critical to preallocate the list size for speed 
res <- vector("list", iter) 
for (i in 1: iter) { 
    res[[i]] <- simulation(J,4* (length(J)^2),0.0007,duration = 10,start) 
} 
str(res) 
res[[1]] 

現在,我不認爲我使用這個挺你最終想要的方式,但也許這會給你足夠的讓你真正想要的。

+0

不,我沒有得到一個錯誤,只是模擬結果向量不會從複製的模擬函數中獲得輸出。 模擬函數輸出一個不同長度的向量,並且運行良好,問題在於cbind和replicate的組合不填充NULL向量 – user124123

+0

'simulation'「輸出一個長度可變的向量」假設這是一個數字或整數矢量,如果長度不同,你將永遠無法將它「綁定」到任何東西上。你可以將它連接到現有的結果:'simulated_results < - c()',然後將'cbind'改爲'c',但是這會給你一個長輸出。如果這不是你想要的,你可能需要使用一個'list',它可以有不同長度的元素。見'?list'。 –

+0

@ user1987097你能按照你想要的方式使用它嗎? –