2016-12-02 67 views
-3

我试图让循环拉正常分布拉100次,但有不同的n值在数据框中给出。我正在使用下面提到的代码,但给我错误。有没有办法在每次迭代中使用不同的n?rmorm For循环不同的n每次迭代

非常感谢, Krina

> head(dat2_Placebo_n) 
Source: local data frame [6 x 2] 

    Repl  N 
    <int> <int> 
1  1 78 
2  2 71 
3  3 60 
4  4 66 
5  5 71 
6  6 82 
> cv.tumor= 0.40 
> sd.tumor<-sqrt(log((cv.tumor)^2+1)) 
> nRep <- 100 
> result<-list() 
> for(i in 1:nRep) { 
+  n<- for(i in 1:dat2_Placebo_n$N) {n<-N[1:100,]} 
+  Log_Tumor <- rnorm(n, log(6.8), sd.tumor) 
+  Tumor <- exp(Log_Tumor) 
+  result[[i]]<-Base_Tumor 
+  } 
Error: object 'N' not found 
In addition: Warning message: 
In 1:dat2_Placebo_n$N : 
    numerical expression has 100 elements: only the first used 
> result <- as.data.frame(unlist(result)) 

回答

0

这个工作。

cv.tumor<- 0.40 
sd.tumor<-sqrt(log((cv.tumor)^2+1)) 
nRep <- 100 
result_Placebo<-list() 
for(i in 1:nRep) { 
    ni<- dat2_Placebo_n$N[[i]] 
    Log_Tumor <- rnorm(ni, log(6.8), sd.tumor) 
    Tumor <- exp(Log_Tumor) 
    result_Placebo[[i]]<-Tumor 
    } 
result_Placebo <- as.data.frame(unlist(result_Placebo))