首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为每个模拟运行循环,并使用这些值创建一个新的向量?

如何为每个模拟运行循环,并使用这些值创建一个新的向量?
EN

Stack Overflow用户
提问于 2020-05-11 20:33:39
回答 1查看 71关注 0票数 0

这是我的数据框:

代码语言:javascript
运行
复制
time<-rep(c(1:5),4)
sim1<-rep(c(paste("sim",1)),5)
sim2<-rep(c(paste("sim",2)),5)
sim3<-rep(c(paste("sim",3)),5)
sim4<-rep(c(paste("sim",4)),5)
sim<-c(sim1,sim2,sim3,sim4)
id<-as.vector(replicate(4,sample(1:5)))

df<-data.frame(time,sim,id)

df$simnu<-as.numeric(df$sim)

它应该看起来像这样:

代码语言:javascript
运行
复制
   time  sim  id simnu
1     1 sim 1  1     1
2     2 sim 1  3     1
3     3 sim 1  2     1
4     4 sim 1  4     1
5     5 sim 1  5     1
6     1 sim 2  1     2
7     2 sim 2  5     2
8     3 sim 2  4     2
9     4 sim 2  2     2
10    5 sim 2  3     2
11    1 sim 3  2     3
12    2 sim 3  3     3
13    3 sim 3  4     3
14    4 sim 3  1     3
15    5 sim 3  5     3
16    1 sim 4  3     4
17    2 sim 4  5     4
18    3 sim 4  2     4
19    4 sim 4  1     4
20    5 sim 4  4     4

我已经创建了这个循环,它通过模拟对数据进行子集,然后计算出我想要的输出:

代码语言:javascript
运行
复制
surveillance<-5
n<-1
simsub<-df[which(df$simnu==1),names(df)%in%c("time","sim","id")]
while (n<=surveillance){
print (n)
rndid<-df[sample(nrow(simsub),1),]
print(rndid)
if(n<rndid$time){
n<-n+1
} else {
tinf<-sum(length(df[which(simsub$time<=n),1]))
prev<-tinf/length(simsub[,1])
print(paste(prev,"prevalence"))
break
}
}

我的问题是,如何为每个模拟运行此循环,并将其值作为向量返回?

EN

回答 1

Stack Overflow用户

发布于 2020-05-12 23:40:16

我给你的建议是看看lapply函数(resp.sapply和vapply),并避免使用while,老实说,在不真正知道代码中发生了什么的情况下提供帮助有点棘手,但无论如何,这里有一个如何使用lapply的示例,但是由于我不知道您的代码应该返回什么,所以我不能确定输出是否正确

我在你的原文中添加了评论和问题,希望这能有所帮助。

代码语言:javascript
运行
复制
# first define a function that takes one simnu and returns whatever you want it to return
my_calc_fun <- function(sim_nr){
  ## you can subset the DF without which, names, or %in% 
  # simres[[i]] <- my_df[which(my_df$simnu==i),names(my_df)%in%c("time","sim","id")] 
  sim_df <- my_df[my_df$simnu == sim_nr, c("time","sim","id")]
  for(n in 1:surveillance){
    ## I'm not sure that is what you meant to do, 
    ## you are sampling the full DF, but you want a sample 
    ## from the subset i.e., simres[[i]]
    # rndid<-my_df[sample(nrow(simres[[i]]),1),] 
    row_id <- sample(nrow(sim_df), 1)
    rndid <- sim_df[row_id, ] 
    if(n >= rndid$time){
      ## what are you trying to sum here?
      ## because you are giving the function one number length(....)
      ## and just like above you are subsetting the full DF here
      # tinf<-sum(length(my_df[which(simres[[i]]$time<=n),1]))
      tinf <- length(sim_df[sim_df$time<=n, 1])
      # is this the value you want to return for each simnu?
      prev <- tinf/length(sim_df["time"]) 
      break
    }
  }
  return(c('simnu'=sim_nr, 'prev' = prev))
}

# apply this function on all values of simnu and save to list
result_all <- lapply(unique(my_df$simnu), my_calc_fun)
result_all
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61729828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档