2014-10-02 52 views
0

我在r中寫入循環,錯誤消息來自最後一行: 「Match.names(clabs,names(xi))中的錯誤: 名稱不匹配以前的名稱「 最後一行試圖保留每個循環的每一行值。感謝大家。如何將每一行添加到循環中的前一行R

a = 20 # resample times 
for (id in unique_index){ 

    jeep_id <- jeep_boot[which(jeep$index == id),] 

    #sample_size is 0.8 of the whole data 
    sample_size <- floor((0.8)*length(jeep_id$PR)) 

    #bootstrap sample means 
    samplemeans = rep(NA, a) 

    for (i in 1:a) { 

    bootsample.i = sample(jeep_id$PR, sample_size, replace=T) 
    samplemeans[i] = mean(bootsample.i) 

    } 
    #combine mean of 20 sample means and stand dieviation of sample means to be one row 
    bootresult_id <- cbind(id, mean(samplemeans), sd(samplemeans)) 
    #retain every row value for each round looping 
    bootresult <- rbind(bootresult, bootresult_id) 
} 
+0

請考慮提供一些示例數據(以及可能的預期結果)。這樣做你會得到更多的迴應。我可以創建一個示例數據來測試它。但是,它可能不會模仿您擁有的實際數據集。 – akrun 2014-10-03 04:04:31

回答

0

啓動for (id in unique_index)循環之前什麼是bootresult? 這一部分我也不清楚,也許這取決於你如何在首位聲明它

這Alternativo的解決辦法是把它聲明爲

bootresult <- matrix(nrow = length(unique_index), ncol=3) 

,取而代之的最後一行

bootresult[id, ] <- bootresult_id 
相關問題