如何从for循环中获取所有结果

时间:2019-05-20 22:48:45

标签: r

我需要从for循环的每个步骤中获取所有结果,而不是从91个向量中仅获取NA和最终数字。

m<-log((189^2)/(sqrt(189^2+127^2)))         
s<-sqrt(log((189^2+127^2)/189^2)) 
logPtr<- c(-2.24,-2.36,-2.37,-2.58,-2.82,-2.86,-3.16,-3.17,-3.47,-3.52,-3.57,-3.83,
        -3.83,-3.84,-3.87,-3.89,-3.89,-3.90,-3.94,-4.03,-4.09,-4.42,-4.53,-4.54,-4.54,
        -4.62,-4.62,-4.68,-4.73,-4.76,-4.84,-4.92,-4.93,-4.95,-4.97,-5.20,-5.25,-5.27,
        -5.39,-5.47,-5.60,-5.83,-5.89,-5.95,-5.96,-6.02,-6.23,-6.38,-6.96,-7.37,-7.90, 
        -8.20,-9.00,-9.00,-9.00)

# transfer rate from meat to portion to eat#  
#55 
#transfer values (observational study)#lambda = Eq.1 outputs * values of 
#conc.meat          
Ptr<-10^logPtr         

p_Ptr<-c(1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
       1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
       1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
       1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
       1/55, 1/55, 
       1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55)         

alpha<-0.145        

beta<-7.59           

v<-c(10^(seq(-2, 7, by=0.1)))    

set.seed(123)   
library(mc2d)    

result <- data.frame(matrix(nrow = 91, ncol = 2))    
colnames(result) <- c("cm", "Mean_Pill")    

for (cm in v)#looping for each value of cm    
     {     
     ndvar(50001)      
     Wc<-mcstoc(rlnorm,meanlog=m,sdlog=s)      
     Nportion<-mcstoc(rpois,lambda=((Wc)*(cm)))      
     P_trans<-mcstoc(rempiricalD, values=c(Ptr), prob=c(p_Ptr))     
     d<-mcstoc(rbinom, size=Nportion, prob=P_trans)     
     Pill<-0.33*(1-((1+d/beta)^(-alpha)))     
     mean_Pill<-mean(Pill, na.rm = TRUE)    
     result[i, 1] <- cm    
     result[i, 2] <- mean(Pill, na.rm = TRUE)    
     }     

print(result)            

2 个答案:

答案 0 :(得分:1)

for循环包括i索引,但实际上并没有遍历i。代替使用cm in v,而是使用i in seq_along(v)

您的全局环境很可能已声明i,这就是为什么您得到一行实际提供结果的原因。

# https://stackoverflow.com/questions/56229092/how-to-obtain-all-results-from-a-for-loop

v<-c(10^(seq(-2, 7, by=0.1))) 

result <- data.frame(matrix(nrow = 91, ncol = 2)) 
colnames(result) <- c("cm", "Mean_Pill")

for (i in seq_along(v)){
  cm <- v[i]
  result[i, 1] <- cm
}

答案 1 :(得分:0)

尝试将for (cm in v)替换为for (i in length(v)),然后在每次要呼叫v[i]时使用cm。代码:

m<-log((189^2)/(sqrt(189^2+127^2)))         
s<-sqrt(log((189^2+127^2)/189^2)) 
logPtr<- c(-2.24,-2.36,-2.37,-2.58,-2.82,-2.86,-3.16,-3.17,-3.47,-3.52,-3.57,-3.83,
           -3.83,-3.84,-3.87,-3.89,-3.89,-3.90,-3.94,-4.03,-4.09,-4.42,-4.53,-4.54,-4.54,
           -4.62,-4.62,-4.68,-4.73,-4.76,-4.84,-4.92,-4.93,-4.95,-4.97,-5.20,-5.25,-5.27,
           -5.39,-5.47,-5.60,-5.83,-5.89,-5.95,-5.96,-6.02,-6.23,-6.38,-6.96,-7.37,-7.90, 
           -8.20,-9.00,-9.00,-9.00)

# transfer rate from meat to portion to eat#  
#55 
#transfer values (observational study)#lambda = Eq.1 outputs * values of 
#conc.meat          
Ptr<-10^logPtr         

p_Ptr<-c(1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
         1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
         1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
         1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 
         1/55, 1/55, 
         1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55, 1/55)         

alpha<-0.145        

beta<-7.59           

v<-c(10^(seq(-2, 7, by=0.1)))    

set.seed(123)   
library(mc2d)    

result <- data.frame(matrix(nrow = 91, ncol = 2))    
colnames(result) <- c("cm", "Mean_Pill")    

for (i in 1:length(v))#looping for each value of cm    
{     
  ndvar(50001)      
  Wc<-mcstoc(rlnorm,meanlog=m,sdlog=s)      
  Nportion<-mcstoc(rpois,lambda=((Wc)*(v[i])))      
  P_trans<-mcstoc(rempiricalD, values=c(Ptr), prob=c(p_Ptr))     
  d<-mcstoc(rbinom, size=Nportion, prob=P_trans)     
  Pill<-0.33*(1-((1+d/beta)^(-alpha)))     
  mean_Pill<-mean(Pill, na.rm = TRUE)    
  result[i, 1] <- v[i]   
  result[i, 2] <- mean(Pill, na.rm = TRUE)    
}     

print(result)