2014-10-02 79 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 
相关问题