【问题标题】:rnorm in For Loop Different n each iterationFor 循环中的 rnorm 每次迭代都不同
【发布时间】:2016-12-02 15:54:49
【问题描述】:

我正在尝试使用 for 循环将正态分布拉 100 次,但使用数据框中给出的不同 n 值。我正在使用下面提到的代码,但给了我错误。有没有办法为每次迭代使用不同的 n?

非常感谢, 克里纳

> 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))

【问题讨论】:

    标签: r for-loop


    【解决方案1】:

    这行得通。

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-16
      • 2016-01-09
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多