2014-05-03 69 views
-2

我有這個for循環,我需要重複(如1000次),並計數p.value的次數高於某個值。我認爲我的計數部分在控制之下,但我無法弄清楚如何重複循環來獲得我可以使用的輸出。R:重複循環和計數結果

leads <-list(lead_one, lead_two, lead_three, lead_four, lead_five, lead_six, lead_seven, lead_eight, lead_nine, lead_ten) 

    for(i in 1:length(leads)){ 
    (ks.test(sample(lead_en, 75), sample(leads[[i]], 75)))$p.value 
    } 

幫助?

更新(更新#2:代碼應該是現在重複性):

我不提供足夠的信息對不起,我是新來編程,並且全新的,以詢問關於編程幫助。讓我再嘗試一次。我想在前置時間向量(lead_en)上運行ks.test,而在「潛在客戶」中對前置時間的所有向量運行。這我可以用上面發佈的for循環來做。然後我想多次運行此循環以計算某個結果的時間。

我已經嘗試過(不計):

lead_en <- c(4, 5, 3, 4, 5, 6, 7, 3, 4, 2, 3) 
lead_one <- c(3, 4, 5, 2, 2, 5, 7, 8, 3, 2, 6) 
lead_two <- c(4, 2, 5, 6, 3, 1, 5, 7, 5) 
lead_three <- c(4, 3, 5, 5, 6, 7, 9, 9, 3, 3, 3) 
lead_four <- c(5, 3, 5, 6, 3, 4, 5, 6) 
lead_five <- c(4, 5, 3, 5, 6) 
lead_six <- c(2, 3, 6, 9, 9, 5, 5, 6, 7, 4, 3, 3) 
lead_seven <- c(3, 4, 5, 5, 6, 3, 3) 
lead_eight <- c(2, 3, 3, 3, 3, 4, 5) 
lead_nine <- c(5, 6, 7, 3, 3, 4) 
lead_ten <- c(5, 3, 4, 5, 6, 7, 7) 
leads <-list(lead_one, lead_two, lead_three, lead_four, lead_five, lead_six, lead_seven, lead_eight, lead_nine, lead_ten) 


d <- 100 # number of replications 
res = matrix(rep(0, nrow=10, ncol=d)) 

for (k in 1:length(d)){ 
    for(i in 1:length(leads)){ 
res[i, k] <- print(ks.test(sample(lead_en, 75), sample(leads[[i]], 75)))$p.value  
    } 
} 

不工作。我希望這能夠澄清我想要做的事情。

+0

什麼是'lead_one','leads_two',三等? –

+0

載體列表。 lead_one等是提前期的向量。我想運行lead_en上的ks.test和引腳上的所有向量。 – user3598791

+0

歡迎來到SO。請[閱讀此](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) –

回答

0

嘗試:

sapply(leads, function(x) ks.test(sample(lead_en, 75), sample(x, 75)))$p.value) 

我想我不能幫助更多的,因爲你沒有提供了足夠的信息。