这是我的数据框:
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)
它应该看起来像这样:
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
我已经创建了这个循环,它通过模拟对数据进行子集,然后计算出我想要的输出:
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
}
}
我的问题是,如何为每个模拟运行此循环,并将其值作为向量返回?
发布于 2020-05-12 23:40:16
我给你的建议是看看lapply函数(resp.sapply和vapply),并避免使用while,老实说,在不真正知道代码中发生了什么的情况下提供帮助有点棘手,但无论如何,这里有一个如何使用lapply的示例,但是由于我不知道您的代码应该返回什么,所以我不能确定输出是否正确
我在你的原文中添加了评论和问题,希望这能有所帮助。
# 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
https://stackoverflow.com/questions/61729828
复制相似问题